Jump to content

Include cases in array only if conditions are true


MegaSonic

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();}}}
Link to comment
Share on other sites

  • 2 weeks later...

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;    }
Link to comment
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.

Link to comment
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.

Link to comment
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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...