0

I need some help ending my program. Is there a command to end it halfway through? In this short guess the number game i made everything goes fine unless the first person wins because it has to complete the loop even after the game has ended

 var rdmNumber = Math.random();
 var timesNumber = rdmNumber * 100;
 var theNumber = Math.round(timesNumber);
 var playerOne = prompt("Player 1 please enter your name...");
 var playerTwo = prompt("Player 2 please enter your name...");
 while (userInput != theNumber) {
     var userInput = prompt(playerOne + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerOne + "      has won!");
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
     var userInput = prompt(playerTwo + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerTwo + "  has won!");
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
 }

4 Answers 4

1

Use break; to break the while loop like this:

 var rdmNumber = Math.random();
 var timesNumber = rdmNumber * 100;
 var theNumber = Math.round(timesNumber);
 var playerOne = prompt("Player 1 please enter your name...");
 var playerTwo = prompt("Player 2 please enter your name...");
 while (userInput != theNumber) {
     var userInput = prompt(playerOne + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerOne + "      has won!");
         break; // it will break the while loop
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
     var userInput = prompt(playerTwo + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerTwo + "  has won!");
         break; // it will break the while loop
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
 }

Or you can define the process as a function when someone win the game return a value and it will end :

function game() {
    var rdmNumber = Math.random();
    var timesNumber = rdmNumber * 100;
    var theNumber = Math.round(timesNumber);
    var playerOne = prompt("Player 1 please enter your name...");
    var playerTwo = prompt("Player 2 please enter your name...");
    while (userInput != theNumber) {
      var userInput = prompt(playerOne + ", Take a Guess (0-100)");
      if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerOne + "      has won!");
         return ; // it will end the function
      } else if (userInput < theNumber) {
         alert("Higher");
      } else {
         alert("Lower");
      }
      var userInput = prompt(playerTwo + ", Take a Guess (0-100)");
      if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerTwo + "  has won!");
         return ; // it will end the function
      } else if (userInput < theNumber) {
         alert("Higher");
      } else {
         alert("Lower");
      }
    }
}

game();
Sign up to request clarification or add additional context in comments.

Comments

0

I think you are looking for continue

while (userInput != theNumber) {
     var userInput = prompt(playerOne + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerOne + "      has won!");
         continue;
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
     var userInput = prompt(playerTwo + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerTwo + "  has won!");
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
 }

Comments

0

The break statement, which was briefly introduced with the switch statement, is used to exit a loop early, breaking out of the enclosing curly braces.

To handle all such situations, JavaScript provides break and continue statements. These statements are used to immediately come out of any loop or to start the next iteration of any loop respectively.

Example

<script type="text/javascript">
<!--
var x = 1;
document.write("Entering the loop<br /> ");
while (x < 20)
{
  if (x == 5){ 
     break;  // breaks out of loop completely
  }
  x = x + 1;
  document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
//-->
</script>

Information courtesy of TutorialsPoint.com

Comments

0

By changing a bit your logic:

Store Player names into an Array, change turn using 0,1,0,1... numbers,
Use only one prompt!! The current player is defined by players[turnNumber]

var rnd = Math.round( Math.random() * 5), // 0 - 5
    res = -1, // Result
    pl = [],  // ["Ethan", "Roko"]
    t = 0;    // turn: 0,1,0,1... (0 = 1st player)

for(var i=0; i<2; i++) pl[i] = prompt("Player"+ (i+1) +", please enter your name:");

while (res !== rnd) {
    res = parseInt( prompt( pl[t] + ", take a Guess (0-5)"), 10);  
    alert( res===rnd ? (res +" is correct! "+ pl[t] +" has won!") : (res<rnd? "Higher" : "Lower"));
    t = ++t % 2; // increment turn and reset to 0 if === 2 Results in: 1,0,1,0...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.