0

Why is my code not working? When I enter a number correctly, it doesn't show me the window.alert that it was right.

Here is the problem: Write a program that chooses a random number between 1 and 10. Give the user 5 guesses (use a loop) to guess the number. If they are wrong, tell them so. If they are right, tell them so and exit the loop. At the end of the program, show them the actual number.

<script>
    var num = Math.floor(Math.random()*10 + 1);

    var guess = parseInt(window.prompt("Enter a number!"));
    for (var x=1; x<=5; x++) {
         if (num == guess) {
             window.alert("You are right!");
             break;
         }
         else {
             window.prompt("You were wrong, try again!");
             continue;
         }
    }
    document.write("The number was "+num);
</script>
4
  • 2
    Worth noting that the code above leads to 6 guesses. Commented Dec 28, 2016 at 15:21
  • Why? I made it 5, how do i fix it? Commented Dec 28, 2016 at 15:25
  • You have the first prompt, then 5 more inside the loop (x = 1 to 5) Commented Dec 28, 2016 at 15:28
  • @BenSnaize has a good point. You allow the user to enter up to 6 guesses, but the last one is never checked for correctness. So even if they get it right the last time, you won't tell them they have. Commented Dec 28, 2016 at 15:29

4 Answers 4

1

You're not using the subsequent guess. You need to update guess when you re-prompt inside the loop:

    guess = parseInt(window.prompt("You were wrong, try again!"));
//  ^^^^^^^^^^^^^^^^^-------------------------------------------^

Example:

var num = Math.floor(Math.random() * 10 + 1);

var guess = parseInt(window.prompt("Enter a number!"));
for (var x = 1; x <= 5; x++) {
  if (num == guess) {
    window.alert("You are right!");
    break;

  } else {
    guess = parseInt(window.prompt("You were wrong, try again!"));
    continue;

  }
}

console.log("The number was " + num);


Separately: Don't use document.write after the main parsing of the page is complete (or, really, at all). Instead, use the DOM to add elements to the page to show what you want to show.

Also, since your loop has nothing else in it, the continue is unnecessary.

And then there's Ben Snaize's point: The code actually lets the user guess six times, although it throws away the final guess.

Sign up to request clarification or add additional context in comments.

Comments

1

Based on @Milaci's version of the answer, with only 5 guesses. Also, it now uses the last guess correctly.

 var num = 5; //Math.floor(Math.random()*10+1);
 var guess = parseInt(window.prompt("Enter a number!"));

 if (num === guess) {
   window.alert("You are right!");
 } else {
   for (var x = 1; x < 5; x++) {
     guess = parseInt(window.prompt("You were wrong, try again!"));


     if (num === guess) {
       window.alert("You are right!");
       break;
     }

   }
 }

 document.write("The number was " + num);

Comments

0

You are generating one random number then getting one guess and then looping five times to see if the two numbers are the same. You should get a new guess inside the loop.

Comments

0

You should use guess twice, one for the first time and other inside a for loop to take a value that user insert. I use a static 5 value to facilitate the test

 var num=5;//Math.floor(Math.random()*10+1);
 var guess=parseInt(window.prompt("Enter a number!"));

for(var x=1; x<=5; x++){

    if (num==guess){
        window.alert("You are right!");
        break;

    }

    else{
        guess=parseInt(window.prompt("You were wrong, try again!"));
    } 
}

document.write("The number was "+num);

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.