/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] not equal to 2 variables

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!

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@dz_boyauthorJun 27.2007 — I think the && works... right?

for(i = 2; i != 3 && i != 4; i + 1)
Copy linkTweet thisAlerts:
@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.
Copy linkTweet thisAlerts:
@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&lt;10)&amp;&amp;(j&lt;12);i++,j=j+2)
{
document.write(i+':'+j+'&lt;br&gt;');
}


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.
Copy linkTweet thisAlerts:
@GaultierzJun 27.2007 — I don't think it will work.

Do you know many time the code will be repeated or when the loop will exit?
Copy linkTweet thisAlerts:
@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.


[CODE]<html>
<head>
<title></title>
<script>
<!--

words = new Array()
words[0] = "heissen";
words[1] = "apfel";
words[2] = "haus";
words[3] = "raus";
words[4] = "bleiben";
words[5] = "finden";

answers = new Array()
answers[0] = "to be called";
answers[1] = "apple";
answers[2] = "house";
answers[3] = "smoke";
answers[4] = "to stay";
answers[5] = "to find";

e = 5; // number of items for randomizers

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)))

ques.value = words[num] + num;
ansr0.value = answers[a0] + a0;
ansr1.value = answers[a1] + a1;
ansr2.value = answers[a2] + a2;
ansr3.value = answers[a3] + a3;
ansr4.value = answers[a4] + a4;
ansr5.value = answers[a5] + a5;
}}}}}
right = document.getElementById('y');
wrong = document.getElementById('n');

function answer0()
{
if(a0===num)
{right.value=parseInt(right.value)+1}
if(a0!=num)
{wrong.value=parseInt(wrong.value)+1}
Ask()
}

function answer1()
{
if(a1===num)
{right.value=parseInt(right.value)+1}
if(a1!=num)
{wrong.value=parseInt(wrong.value)+1}
Ask()
}

function answer2()
{
if(a2===num)
{right.value=parseInt(right.value)+1}
if(a2!=num)
{wrong.value=parseInt(wrong.value)+1}
Ask()
}

function answer3()
{
if(a3===num)
{right.value=parseInt(right.value)+1}
if(a3!=num)
{wrong.value=parseInt(wrong.value)+1}
Ask()
}

function answer4()
{
if(a4===num)
{right.value=parseInt(right.value)+1}
if(a4!=num)
{wrong.value=parseInt(wrong.value)+1}
Ask()
}

function answer5()
{
if(a5===num)
{right.value=parseInt(right.value)+1}
if(a5!=num)
{wrong.value=parseInt(wrong.value)+1}
Ask()
}
-->
</script>
</head>
<body>
<table border="1">
<tr>
<td rowspan="2"><input type="button" value="Click" onClick="Ask()"></td>
<td rowspan="6"><center>
<input id="ques" style="font-family: arial; font-size: 40pt; color: #0000A0; border-color: white; border-width: 0px; height: 42pt; width: 350px; text-align: center;"></td>

<td><input type="button" value=" " onClick="answer0()"><input id="ans0" style="font-family: arial; color: black; border-color: white; border-width: 0px;">
</td>
</tr>
<tr>
<td><input type="button" value=" " onClick="answer1()"><input id="ans1" style="font-family: arial; color: black; border-color: white; border-width: 0px;">
</td>
</tr>
<tr>
<td rowspan="2">button</td>
<td><input type="button" value=" " onClick="answer2()"><input id="ans2" style="font-family: arial; color: black; border-color: white; border-width: 0px;">
</td>
</tr>
<tr>
<td><input type="button" value=" " onClick="answer3()"><input id="ans3" style="font-family: arial; color: black; border-color: white; border-width: 0px;">
</td>
</tr>
<tr>
<td rowspan="2">button</td>
<td><input type="button" value=" " onClick="answer4()"><input id="ans4" style="font-family: arial; color: black; border-color: white; border-width: 0px;">
</td>
</tr>
<tr>
<td><input type="button" value=" " onClick="answer5()"><input id="ans5" style="font-family: arial; color: black; border-color: white; border-width: 0px;">
</td>
</tr>
<tr>
<td colspan="3"><center>Correct &rarr; <input type="text" id="y" disabled="disabled" size="2" value="0">&nbsp;&nbsp;&nbsp;<input type="text" id="n" disabled="disabled" size="2" value="0"> &larr; Incorrect</td>
</tr>
</table>
</body>
</html>

[/CODE]
Copy linkTweet thisAlerts:
@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.
Copy linkTweet thisAlerts:
@dz_boyauthorJun 27.2007 — That's the problem I've encountered now. I changed all the for() loops into if() statements, but that doesn't seem to be working either. See my new post at http://www.webdeveloper.com/forum/showthread.php?t=152825. Thanks
×

Success!

Help @dz_boy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...