SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Basic Project if/else

    The flow of your program should look like this:

    Guess the secret number (1-100)
    You have 3 turns left.

    Too HIGH. Guess again
    You have 2 turns left.

    Too LOW. Guess again
    You have 1 turn left.

    You are correct! It took you 3 turns!

    My program won't run at all and I can't find the problem. Also, am I going about it the right way or is there an easier shorter way of doing this than if/else?

    Code:
      function main(){
    
        var secretNumber = "23"; //integer, holds correct answer
        var userGuess = "Welcome to the Number Guesser! " +" Guess the secret number between 1-100" +"\n You have 3 turns to guess the secret number."; //integer, holds guess answer
    
        while( userGuess != secretAnswer){
        
        if(userGuess == secretNumber){
          alert("Impressive! You guessed the secret number on your first turn.");
        
        }else{ 
        
        if(userGuess < secretNumber){
          alert("Too LOW.  Guess again." + '\n' + "You have 2 turn(s) left.");
    
        }else{
      
        if(userGuess > secretNumber){
          alert("Too HIGH.  Guess again." + '\n' + "You have 2 turn(s) left.");
    
      } // end if
      } // end if
      } // end if  
       
         userGuess = prompt("Guess the secret number (1-100). you have 2 turns left");
    
        if(userGuess == secretNumber){
          alert("Great! You got that on your second turn.");
        
        }else{ 
        
        if(userGuess < secretNumber){
          alert("Too LOW.  Guess again." + '\n' + "You have 1 turn(s) left.");
    
        }else{
      
        if(userGuess > secretNumber){
          alert("Too HIGH.  Guess again." + '\n' + "You have 1 turn(s) left.");
    
    
      } // end if
      } // end if
      } // end if  
    
     
        userGuess = prompt("Guess the secret number (1-100). you have 1 turns left");
    
        if(userGuess == secretNumber){
          alert("Nice guess! You got that in your last try.");
        
        }else{ 
        
        if(userGuess < secretNumber){
          alert("Too LOW.  Game over.");
    
        }else{
      
        if(userGuess > secretNumber){
          alert("Too HIGH.  Game over.");
    
      } // end if
      } // end if
      } // end if 
    
      } // end while
        
      } //end main function

  2. #2
    SitePoint Enthusiast SoMBrA's Avatar
    Join Date
    Oct 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello there!
    Your first mistake is that you checking a wrong variable, you comparing to "secretAnswer" while you are working with "secretNumber" (just at the while line).

    After that i would recommend you to use switch/case statements instead all of those if/else.

    And well the last thing would recommend you to work with numbers instead of strings, so force transform of the variables (parseInt the input var and set the secretNumber = 23 without double quotes)

    Here you have some code i wrote just to give you a small idea on how you could "improve" your code (imho of course )

    Code:
      
    function main(){
    	var i = 0;
    	var o = 3;
        var secretNumber = 23; //integer, holds correct answer
        var userGuess = "Welcome to the Number Guesser! " +" Guess the secret number between 1-100" +"\n You have 3 turns to guess the secret number."; //integer, holds guess answer
    
        while( (userGuess != secretNumber) && (i < 3) ){
    	i++; o--;
    	switch(i) {
    		case 1:
    			userGuess = userGuess;
    			break;
    		case 2:
    			userGuess = "Try 2/3"
    			break;
    		case 3:
    			userGuess = "Try 3/3"		
    			break;
    	}
        userGuess = parseInt(prompt(userGuess));
        if(userGuess == secretNumber){
          alert("Impressive! You guessed the secret number on " + i + " trys");
        
        }else{ 
        
        if(userGuess < secretNumber){
    	  if (o == 0) { alert("Game Over"); continue; }	
          alert("Too LOW.  Guess again." + '\n' + "You have " + o + " turn(s) left.");
    
        }else{
      
        if(userGuess > secretNumber){
    	  if (o == 0) { alert("Game Over"); continue; }
          alert("Too HIGH.  Guess again." + '\n' + "You have " + o + " turn(s) left.");
    
      } // end if
      } // end if
      } // end if  
      }
      }

  3. #3
    SitePoint Member
    Join Date
    May 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's another approach that's a little more flexible:

    Code:
    var secret = 23; //secret answer
    var guesses = 3; //number of guesses allowed
    var message = "Guess the secret number (1-100).";
    var attempts = 0; //set initial attempts to none
    
    //see if the guess is a number at all
    //or whether it's too high or low
    function compare(guess,secret) {
      if (isNaN(guess) || !guess) {
        return "That's not a number!";
      } else {
        return (guess<secret) ? "Too LOW." : "Too HIGH.";
      }
    }
    
    do {
      if (attempts>0) {
        result = compare(guess,secret);
        message = result + "\nGuess again\n";
        message += "You have " + (guesses-attempts) + " turn";
        message += (guesses-attempts > 1) ? 's' : '';
        message += " left.";
      }
      var guess=prompt(message);
      attempts++;
    } while (guess!=secret && attempts<guesses)
    
    if (guess==secret) {
      switch(attempts) {
        case 1 :
          alert("Impressive! You guessed the secret number on your first turn.");
          break;
       case 2 :
          alert("Great! You got that on your second turn.");
          break
        default :
          alert("Nice guess! You got that in your last try.");
      }
    } else {
      alert(compare(guess,secret) + "  Game over.");
    }
    --dave

  4. #4
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow! neat... learned something new! This is for an online beginners course and have not been taught switch/case statements, so unfortunately I can't use it. By the way, how would you do an error check to see if the user entered a number between 1-100 and not like 1236? Do you know how to use the isNaN function (our teacher left that as the hint)?

  5. #5
    Non-Member
    Join Date
    Aug 2007
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's useful tut


Bookmarks

Posting Permissions

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