I’m sort of new at this and I’m doing a for() loop. Is there a way to set a condition to make sure that a value is not equal to [I]two[/I] variables? Would this work: for(i = 2; i != 3,4; i+1), preventing the loop from setting i as 3 or 4. Please help- thanks!
@konithomimoJun 27.2007 — #You seem to not understand how a for loop works. It is true that the second term is a condition that says when to run the loop, but it is for determining when to stop running the loop. Your statement does that, but it will start i at 2 and then stop the loop. You probably are trying to do this:
for(var i=2; i<10;i++)
{
if(i!=3&&i!=4)
{
your code here
}
}
your code just gives two times for ending the loop, the first of which will always be met first.
@konithomimoJun 27.2007 — #To clarify one thing, you can have two conditions for ending a loop. For example:
var i = 2; var j=0; for(i; (i<10)&&(j<12);i++,j=j+2) { document.write(i+':'+j+'<br>'); }
that code will end with i being 7 and j being 10, because the next iteration of the loop makes j=12, which causes j<12 to be false, thus ending the loop. If you change j<12 to j<20, then it will end when i=9 and j=14, because the next iteration of the loop will make i=10, which causes the term i<10 to be false.
@dz_boyauthorJun 27.2007 — #I appreciate all the advice. The loop that I was trying to accomplish (far more complicated than the example I posted earlier) hasn't yet worked, so would you mind looking at that as well... and maybe the rest of the code as well? The objective is to create "flashcards" that selects 6 random answers and then should tally each time the correct one is selected. Unfortunatley, something seems awry because the right ones are often considered wrong and I don't know why.... good luck and thanks.
function Ask() { var ques = document.getElementById('ques'); var ansr0 = document.getElementById('ans0'); var ansr1 = document.getElementById('ans1'); var ansr2 = document.getElementById('ans2'); var ansr3 = document.getElementById('ans3'); var ansr4 = document.getElementById('ans4'); var ansr5 = document.getElementById('ans5');
num = Math.round(Math.random()*e);
a0 = Math.round(Math.random()*e); for (a1=(Math.round(Math.random()*e)); (a1!=a0); a1=(Math.round(Math.random()*e))) { for (a2=(Math.round(Math.random()*e)); (a2!=a0)&&(a2!=a1); a2=(Math.round(Math.random()*e))) { for (a3=(Math.round(Math.random()*e)); (a3!=a0)&&(a3!=a1)&&(a3!=a2); a3=(Math.round(Math.random()*e))) { for (a4=(Math.round(Math.random()*e)); (a4!=a0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3); a4=(Math.round(Math.random()*e))) { for (a5=(Math.round(Math.random()*e)); (a5!=a0)&&(a5!=a1)&&(a5!=a2)&&(a5!=a3)&&(a5!=a4); a5=(Math.round(Math.random()*e)))
@gooncorpJun 27.2007 — #according to HTML-Kit, when you click on the buttons on the right, the variables a0 - a5 are undefined. have a look through your code, and fix this.