Jump to content
MegaSonic

Include cases in array only if conditions are true

Recommended Posts

Hello,

 

I am still rather new to JavaScript. I am working on making a study tool for school. Below I have a sample of my code. I would like for a case to included in the array WordList only if a condition is true. I have check-boxes for each case. What I have currently is a poor workaround where if for example case 1's box is not checked it will shuffle again and then run the function again. Instead, I would like to make it so that if case 1's box is not checked it won't be included in the array at all, if that's possible. I'm sorry I can't explain this very well. Thanks in advance for any input.

Array.prototype.randomize = function(){    var i = this.length, j, temp;    while ( --i )    {        j = Math.floor( Math.random() * (i - 1) );        temp = this[i];        this[i] = this[j];        this[j] = temp;    }}var WordList = [0,1];WordList.randomize();function Word() {var WordL = WordList [0];switch (WordL) {    case 0:        if(document.getElementById('micro').checked) {        WordL = MicroList.randomize(); Micro();}        else {WordList.randomize(); Word();}        break;    case 1:        if(document.getElementById('immuno').checked) {        WordL = ImmunoList.randomize(); Immuno();}        else {WordList.randomize(); Word();}}}

Share this post


Link to post
Share on other sites

The first thing the Word function should do is get a list of everything that is checked, build the array with only those items, then randomize it.

Share this post


Link to post
Share on other sites

The first thing the Word function should do is get a list of everything that is checked, build the array with only those items, then randomize it.

 

Okay. I've made some changes, but now I'm having trouble with the randomization. My WordList.randomize function isn't doing what I expected. AntifungalList.randomize (and two other randomize functions which aren't shown) is working as expected. The overall goal of this part of my script is to bring up the name of a medication. The medications come from three different classes (antibiotic, antiviral, or antifungal). When I have all the checkboxes checked, it will always bring up an antiviral medication and never antibiotic or antifungal. However, it does randomize within each category.

function Word() {var WordList = [];var list = document.forms['check'].elements['section'];for (var i=0, arr=list.length;i<arr;i++) {    if (list[i].checked) {        WordList.push(list[i].value);        }    }WordList.randomize();var WordL = WordList[0];document.getElementById('cue2').innerHTML = WordList[0];switch (WordL) {    case "antibiotics":        WordL = AntibioticList.randomize(); Antibiotics();        break;    case "antivirals":        WordL = AntiviralList.randomize(); Antivirals();        break;    case "antifungals":        WordL = AntifungalList.randomize(); Antifungals();        }}var AntifungalList  = [0,1,2,3,4,5]AntifungalList.randomize();function Antifungals() {    var AntifungalL = AntifungalList[0];    var explain, value    switch (AntifungalL) {        case 0:            AntifungalL = '<img src="Game_Cards/amphotericin_b.jpg" border="2"/>';            name = "Amphotericin_B";            explain = "1st day";            value = 10;            break;        case 1:            AntifungalL = '<img src="Game_Cards/nystatin.jpg" border="2"/>';            name = "Nystatin";            explain = "2nd day";            value = 10;            break;        case 2:            AntifungalL = '<img src="Game_Cards/fluctosine.jpg" border="2"/>';            name = "5_Flucytosine";            explain = "converted to 5-Fluorouracil";            value = 10;            break;        case 3:            AntifungalL = '<img src="Game_Cards/fluconazole.jpg" border="2"/>';            name = "Fluconazole";            explain = "4th day";            value = 10;            break;        case 4:            AntifungalL = '<img src="Game_Cards/ketoconazole.jpg" border="2"/>';            name = "Ketoconazole";            explain = "5th day";            value = 10;            break;        case 5:            AntifungalL = '<img src="Game_Cards/itraconazole.jpg" border="2"/>';            name = "Itraconazole";            explain = "6th day";            value = 10;            break;        }    document.getElementById('treatment_name').innerHTML = name;    document.getElementById('explanation').innerHTML = explain;    }

Share this post


Link to post
Share on other sites

My WordList.randomize function isn't doing what I expected.

Have you checked what WordList contains before and after randomizing?

Share this post


Link to post
Share on other sites

Have you checked what WordList contains before and after randomizing?

 

Before randomization, WordList contains: antibiotics, antivirals, antifungals

This is the order of the checkboxes too.

 

After randomization, WordList contains: antivirals, antifungals, antibiotics

I see that antibiotics is pushed to the back and the result is always the same.

Share this post


Link to post
Share on other sites

What about the other arrays that get randomized, do they also end up in the same order? If so, it sounds like the randomize function isn't actually random.

Share this post


Link to post
Share on other sites

From my AntiviralList.randomize I obtained these results:

1st time - 2,3,1,5,4,0

2nd time - 4,1,0,2,3,5

3rd time - 3,0,2,5,1,4

 

I ran a few tests and I think I'm starting to understand what's going on. Because the randomize function has very simplistic rules, it looks like it doesn't work with 3 or less items. In the Tryit Editor I created a sample array with three cases and it always came back 1,2,0. When I added a fourth item it came back as either 3,2,0,1 or 1,2,3,0 every time.

Share this post


Link to post
Share on other sites

Yeah that's not the best randomize function, it looks like they were trying to do a version of the Fisher-Yates shuffle but they screwed it up by doing a pre-increment on the index at the start of the while loop. You can see a corrected version here:http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...