www.webdeveloper.com
Results 1 to 9 of 9

Thread: Loop until one of multiple conditions are met

  1. #1
    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"){
    alert("That's not right!");
    var x = prompt("Pick 1, 2 or 3");

    }
    alert("Cool, good job!")

    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"){
    alert("That's not right!");
    var x = prompt("Pick 1, 2 or 3");

    }
    alert("Cool, good job!")

    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. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Something like this:

    Code:
    <script type="text/javascript">
    
    	var x;
    
    	do {
    		if (x) {alert('That\'s not right!');}
    		x = prompt('Pick 1, 2 or 3') || '0';
    	} while(
    		x != 1 &&
    		x != 2 &&
    		x != 3
    	);
    
    	alert('Cool, good job!');
    
    </script>

  3. #3
    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");

    alert('Cool, good job!');

  4. #4
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    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. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,518
    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;
    		if(x==-1){alert('That\'s not right!');}
    }
    alert('Cool, good job!');
    much easier to follow the logic.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,518
    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;
    		if(x==-1){alert('That\'s not right!');}
    }
    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, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

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

    var x;

    do {
    if (x) {alert('That\'s not right!');}
    x = prompt('Pick 1, 2 or 3') || '0';
    } while(
    x != 1 &&
    x != 2 &&
    x != 3
    );

    alert('Cool, good job!');

    </script>

    That is good...

  8. #8
    Join Date
    Dec 2013
    Posts
    8
    Quote Originally Posted by \\.\ View Post
    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;
    		if(x==-1){alert('That\'s not right!');}
    }
    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. #9
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles