# Thread: Loop until one of multiple conditions are met

1. Registered User
Join Date
Dec 2013
Posts
8

## Loop until one of multiple conditions are met

Hello all,

I am learning JavaScript so maybe this is a no brainer for some. I am trying to make a while loop continue prompting the user until the user picks one of three choices. I am able to get the code to work fine with a single condition:

var x = prompt("Pick 1, 2 or 3");

while(x != "1"){
var x = prompt("Pick 1, 2 or 3");

}

As you can see I am asking the use to select 1, 2, or 3 but the condition is only met if the user selects 1. I tried to incrementally get the code to work with a change in the condition as follows:

var x = prompt("Pick 1, 2 or 3";

while(x != "1" || x != "2"){
var x = prompt("Pick 1, 2 or 3");

}

Sadly I create and infinite loop. I have tried many different methods and I keep failing. I think it is time for help. Can anyone show me please how to do this correctly? Much thanks.

Best Regards,
Aaron.

2. Something like this:

Code:
```<script type="text/javascript">

var x;

do {
x = prompt('Pick 1, 2 or 3') || '0';
} while(
x != 1 &&
x != 2 &&
x != 3
);

</script>```

3. Registered User
Join Date
Dec 2013
Posts
8
Thanks a bunch. I changed it a bit to fit the tutorial I was taking. Can you help clear up my mis-understanding of this? When I first attempted to do this, I was using a do while just as you did. It was failing because I was putting or || operators instead of and && as you did. I thought && means all conditions must be met and || can be one of many. Help me to understand how this works. Please explain.

var x;

do {

x = prompt("Pick Rock, Paper or Scissors");

} while ( x != "rock" && x != "paper" && x != "scissors");

4. Because Bionoid is busy at the moment, I'll try to explain it to you the best as I can and hope that Bionoid wouldn't mind it.

At the beginning the variable x is declared but not initialized. That means that it has been assigned the value of undefined by JS automatically and undefined is a falsy value.

So you're having condition which says: while x is not equal to 1 and 2 and x is not equal to 3 repeat the code inside the body of the loop.

Then you have the body of a do loop. You are asked to input the value of x and you have the || operator. The OR operator evaluates its operand on the left side and if it is a truthy value it returns that value and if it's not it returns 0.

This means that as long as you don't input 1, 2 or 3 the condition will be true or if you input one of the falsy values the x will evaluate to 0.

5. This...
Code:
```...}while(
x != 1 &&
x != 2 &&
x != 3)
);```
Simpler
Code:
`...}while( x<1 || x>3 )`
For a beginner

Code:
```var x=-1;
while(x<0){
if (x==-1) {
x = prompt('Pick 1, 2 or 3');
}
if(x==0) x=-1;
if(x>3) x=-1;
}
much easier to follow the logic.

6. When it comes to textual inputs, you need to then start to use methods to alter your input strings.

Code:
```var x=-1;
while(x<0){
if (x==-1) {
c = prompt('Pick Rock, Paper or Scissors');
}
if( c.toLowerCase() == "rock" ) x = 1;
if( c.toLowerCase() == "paper" ) x = 1;
if( c.toLowerCase() == "scissors" ) x = 1;
}
alert('You chose ' + c );```
The toLowerCase() method makes testing of a string value much easier as you would have to test for all scenarios like Rock, rOck, roCk, rocK and ROck, etc to ensure that you capture the test value, when you use toLowerCase as you can see, you only need to test one value and then the original output can be echoed with the user none the wiser

To use a more advanced script, you could use the do{}while() method and the use of logical testing, as you are a beginner, testing things generally will be as I have demonstrated a simple test method and not a more complex compound method as has been previously demonstrated and using it when you may not have been taught it yet would definitely arouse your tutors suspensions unless when asked you were truthful on getting help.

7. Member
Join Date
Dec 2013
Location
Delhi
Posts
2
<script type="text/javascript">

var x;

do {
x = prompt('Pick 1, 2 or 3') || '0';
} while(
x != 1 &&
x != 2 &&
x != 3
);

</script>

That is good...

8. Registered User
Join Date
Dec 2013
Posts
8
Originally Posted by \\.\
When it comes to textual inputs, you need to then start to use methods to alter your input strings.

Code:
```var x=-1;
while(x<0){
if (x==-1) {
c = prompt('Pick Rock, Paper or Scissors');
}
if( c.toLowerCase() == "rock" ) x = 1;
if( c.toLowerCase() == "paper" ) x = 1;
if( c.toLowerCase() == "scissors" ) x = 1;
}
alert('You chose ' + c );```
The toLowerCase() method makes testing of a string value much easier as you would have to test for all scenarios like Rock, rOck, roCk, rocK and ROck, etc to ensure that you capture the test value, when you use toLowerCase as you can see, you only need to test one value and then the original output can be echoed with the user none the wiser

To use a more advanced script, you could use the do{}while() method and the use of logical testing, as you are a beginner, testing things generally will be as I have demonstrated a simple test method and not a more complex compound method as has been previously demonstrated and using it when you may not have been taught it yet would definitely arouse your tutors suspensions unless when asked you were truthful on getting help.

Yes your right, for a beginner (me), this logic was much easier to follow. Thanks.

9. Registered User
Join Date
Dec 2013
Posts
8
Just wanted to say thank God for this forum and the assistance I found here. I learned a lot from this. Much appreciated.