Jump to content

MegaSonic

Members
  • Content count

    5
  • Joined

  • Last visited

Community Reputation

0 Neutral

About MegaSonic

  • Rank
    Newbie
  • Birthday 10/31/1988

Profile Information

  • Interests
    Medicine, Education, Web Development
  1. MegaSonic

    Include cases in array only if conditions are true

    Thanks for the help! Everything works fine now.
  2. MegaSonic

    Include cases in array only if conditions are true

    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.
  3. MegaSonic

    Include cases in array only if conditions are true

    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.
  4. MegaSonic

    Include cases in array only if conditions are true

    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; }
  5. 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();}}}
×