Luke
Luke

Reputation: 990

while loop doesnt stop even after condition has been fulfilled

Functionality:

User is to input the color based on a guessing game. Prompt will keep prompt when the answer is wrong, prompt will only exit when the user set the correct answer.

WHat I have done: Have made use of a while loop to check the condition. In while loop, have set the while loop to finish only when it is equivalent to target, and target is a randomly picked color by the system

Issue: Prompt initially set the color as the answer but prompt kept looping even after user has set the correct answer.

How to exit loop when one of the color is set?

<html>

<head>
  <title>Color Guessing Game</title>
</head>

<body onload="do_game()">
  <script>
    function do_game() {
        var color = ["blue", "cyan", "gray", "green", "magenta", "orange", "red", "white", "yellow"];
        var guess_input_text, guess_input, finished = false,
          target, guesses = 0;

        var target_index = color[0, 8];
        target = target_index;
        alert(target);
        while (!finished) {
          guess_input_text = prompt("I am thinking of these colors:" +
            "\n\n blue, cyan, gray, green, magenta, orange, red, white, yellow" + "\n\n what color am I thinking of?");
          guess_input = parseInt(guess_input_text);
          guesses += 1;
          finished = target;
        }
      }
  </script>
</body>

</html>

Upvotes: 0

Views: 512

Answers (3)

prem kumar
prem kumar

Reputation: 5877

Another way of doing it without finished variable. Using break

var target_text = color[0, 8];
...
while (true) {
      guess_input_text = prompt("I am thinking of these colors:" +
        "\n\n blue, cyan, gray, green, magenta, orange, red, white, yellow" + "\n\n what color am I thinking of?");

      guesses += 1;
      if(guess_input_text == target_text){
       //guessed correct answer
       break;
      }
}

Upvotes: 1

Miles Grimes
Miles Grimes

Reputation: 442

Your code doesn't do what you think it does.

This doesn't get the index of a random value, it gets the last value.

var target_index = color[0,8];

This will get a random array value: var rand = color[Math.floor(Math.random() * color.length)];

Once you have the winning value, there's no point in doing an index comparison. You can simply compare strings, as prompt() returns the string of the input response (modified prompt text for brevity).

var finished = false;
while (!finished)
{
    var input = prompt("What color am I thinking of?");
    finished = (input.toLowerCase() === rand);
}

Upvotes: 1

Orell Buehler
Orell Buehler

Reputation: 116

Try this code which controls every round of the loop if the input is the same as the target and if they're the same then finished gets true:

function do_game() {
    var color = ["blue", "cyan", "gray", "green", "magenta", "orange", "red", "white", "yellow"];
    var guess_input_text, guess_input, finished = false,
      target, guesses = 0;

    var rnd = Math.floor((Math.random() * 9) + 0); //Makes a random number between 0 and 8
    target = color[rnd]; //sets the target to a random color from the color array
    while (!finished) {
      guess_input_text = prompt("I am thinking of these colors:" +
        "\n\n blue, cyan, gray, green, magenta, orange, red, white, yellow" + "\n\n what color am I thinking of?"); //gets alert-input from user
      guesses += 1;
      if(guess_input_text == target){//checks if input from user and target are the same
        finished = true;
      }
    }
  }

Upvotes: 2

Related Questions