0

So as a practice, I made a guess game in JavaScript where you have to guess the randomly generated number between 1 and 10 in three tries. It worked fine, but when the three tries are completed (or the user guesses the number), it starts all over again. I want it to stop when the above given circumstances are met.

Here is the code:

function runGame() {

  var isPlaying = true;
  var num = Math.floor(Math.random() * 10);
  var guess;
  var tries = 3;

  alert("You have 3 chances to guess a mindset between 1 and 10!");

    while (tries >= 0) {

    guess = prompt("Enter a guess:");

    if (guess > num) {
      alert("Too high!");
    }

    else if (guess < num) {
      alert("Too low!");
    }  

    else {
      alert("Exactly! " + num + " it is! You've won!");
    }
    tries--;
  }
  
  if (tries == 0) {
    isPlaying = false;
  }
}

while (isPlaying = true) {

  runGame();

}
1
  • remove while loop from runGame() i.e change while (isPlaying = true) { runGame(); } to runGame() and also change while (tries >= 0) to while (tries > 0). Remove = Commented May 7, 2021 at 8:08

3 Answers 3

1

A few things:

Put isPlaying variable global. Although you can remove it entirely as well. You already have a while loop condition that does the same thing. Remove the equal sign when comparing your tries to zero. Otherwise it will run still when the tries reached zero. Use a break statement when the user guessed the right answer, otherwise it will still run after guessing.

Other than those your code is fine. Here's the final code:

function runGame() {

  var num = Math.floor(Math.random() * 10);
  var guess;
  var tries = 3;

  alert("You have 3 chances to guess a mindset between 1 and 10!");

    while (tries > 0) {

    guess = prompt("Enter a guess:");

    if (guess > num) {
      alert("Too high!");
    }

    else if (guess < num) {
      alert("Too low!");
    }  

    else {
      alert("Exactly! " + num + " it is! You've won!");
        break;
    }
    tries--;
  }
}

runGame();

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

Comments

1

= in JavaScript is used for assigning values to a variable. == in JavaScript is used for comparing two variables.

So change isPlaying = true to isPlaying == true and it will be fine.

while (tries >= 0) here you can use just while (tries > 0)

You can also declare these variables outside of the function but it's not necessary.

  var isPlaying = true;
  var tries = 3;
function runGame() {
  var num = Math.floor(Math.random() * 10);
  var guess;
  
  alert("You have 3 chances to guess a mindset between 1 and 10!");

    while (tries >= 0) {

    guess = prompt("Enter a guess:");

    if (guess > num) {
      alert("Too high!");
    }

    else if (guess < num) {
      alert("Too low!");
    }  

    else {
      alert("Exactly! " + num + " it is! You've won!");
    }
    tries--;
  }
  
  if (tries == 0) {
    isPlaying = false;
  }
}

while (isPlaying == true) {
  runGame();
}

Comments

0

Remove the isPlaying and call runGame() directly, not in a while loop, You can break the execution if chances gets done and rest tries if the user wins

function runGame() {

    var num = Math.floor(Math.random() * 10);
    var guess;
    var tries = 3;

    alert("You have 3 chances to guess a mindset between 1 and 10!");

    while (tries >= 0) {

        if (tries == 0) {
            alert("You have finished your chances");
            break;
        }

        guess = prompt("Enter a guess:");

        if (guess > num) {
            alert("Too high!");
        } else if (guess < num) {
            alert("Too low!");
        } else {
            alert("Exactly! " + num + " it is! You've won!");
            // reset tries back to three
            tries = 3;
        }

        tries--;

    }

}

runGame();

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.