0

I am trying to make a guessing game with simple java script. the professor gave us a guide, but i am still a bit confused. Based on the guide, we are to use a do/while loop, and within that loop, use the if/else if conditional statement, here is what i have so far. i've tried changing it so many times, and all i end up with is a never ending loop, even after the number is guessed, it doesnt stop

<body>
    <p>
        I'm thinking of a number between 1 and 100, try to guess it! </br>

        <script>
            var number = Math.floor(Math.random() * 100 + 1)
        //guess variable to store the guessed number by user
            var guess 
        //output to store output to the user
            var output 
        //if the user guessed the number or not, initialize it to false
            var guessed = false

        //do/while loop, while condition is based on if the user NOT guessing the number (e.g. guessed == false)
        do {
            guess = prompt ("Think of a number between 1 and 100, what is your number?");
            document.write ("You guessed the number " + guess + "<br/>");
            if (guess > number) {
                document.write ("You guessed too high, think smaller" + "<br/>");
                guessed = false
                }
            else if (guess < number){
                document.write ("You guessed too low, think bigger" + "<br/");
                guessed = false
                }
            else {
                alert("You guessed the right number!")
                guessed = true}
        }
        while (guessed = false) 


        </script>
    </p>
</body>
  • 1
    `=` is an assignment. `==` and `===` are comparison – j08691 Jun 13 '16 at 17:01
  • 2
    `=` is assignment, `==` is equality. You need to change it to `while (guessed == false)`. Right now, you're setting it to `false` on every loop. You could also simplify it to `while (!guessed)` by using the "not" (`!`) operator. – Mike Cluck Jun 13 '16 at 17:02
  • ok, so i did that and it worked, thanks! Now, how do i get it to stop? After the user guesses the number it just goes back to making him/her guess again without picking a new number – Maria G. Rios Arias Jun 13 '16 at 17:05

4 Answers4

0

I believe that you are missing a semi-colon here :

while(guessed == false);

This is just an requirement specific to the do-while loop and does not occur in the syntax for other loops.

Ryan Rao
  • 15
  • 1
  • 6
0

Your code has some issues.

First of all you need to change while (guessed = false) into while (guessed === false) because it should be a comparision, hence you have to use a comparator.

Secondly, you are using document.write(). This will not work, because the HTML won't change until the user breaks out of the loop. It would be a good idea to use alert() instead. Here is a working example:

var number = Math.floor(Math.random() * 100 + 1)
  //guess variable to store the guessed number by user
var guess
  //output to store output to the user
var output
  //if the user guessed the number or not, initialize it to false
var guessed = false

//do/while loop, while condition is based on if the user NOT guessing the number (e.g. guessed == false)
do {
  guess = prompt("Think of a number between 1 and 100, what is your number?");
  if (guess > number) {
    alert("You guessed too high, think smaller");
    guessed = false
  } else if (guess < number) {
    alert("You guessed too low, think bigger");
    guessed = false
  } else {
    alert("You guessed the right number!")
    guessed = true
  }
}
while (guessed === false)
tjespe
  • 704
  • 7
  • 17
0

In your condition at while(guessed = false) you use one equal-signe (=), witch is forever true. you must write (guessed == false) or (guessed === false). for trying i would do ist so:

I'm thinking of a number between 1 and 100, try to guess it!

<script type="text/javascript">
    var number = Math.floor(Math.random() * 100 + 1);
    // Just for Test
    document.getElementById('test').innerHTML += '<br />' + number;
    //guess variable to store the guessed number by user
    var guess;
    //output to store output to the user
    var output;
    //if the user guessed the number or not, initialize it to false
    var guessed = false;

    //do/while loop, while condition is based on if the user NOT guessing the number (e.g. guessed == false)
    do {
        guess = prompt("Think of a number between 1 and 100, what is your number?");
        document.getElementById('test').innerHTML += '<br />' + "You guessed the number " + guess;
        if (guess > number) {
            document.getElementById('test').innerHTML += '<br />' + "You guessed too high, think smaller";
            guessed = false;
        }
        else if (guess < number) {
            document.getElementById('test').innerHTML += '<br />' + "You guessed too low, think bigger";
            guessed = false;
        }
        else {
            alert("You guessed the right number!")
            guessed = true;
        }
    }
    while (guessed == false)
</script>

K. Aryubi
  • 1
  • 1
0

You're missing a ton of semi-colons which should be added though your code will still run without them. And like everyone else said you are assigning guessed to false at the end of your do while loop, therefore it will never break out. To check a condition you should use 3 equal signs (this is strict comparison). More on that here. It is debatable if document.write should be used. Reason for that can be found here. Also, when testing your code it worked intermittently, so I added a div with the id of hints. Then I grabbed that element with var elHints = document.getElementById('#hints'); Then to add content to the element hints simply do elHints.textContent += .... The plus equals (+=) adds content to the element without overwriting existing content. In css I added a style to #hints: white-space: pre-line; this allows line breaks (\n) to the div when adding content using textContent. More on that here.

Here is the full javascript code:

var number = Math.floor(Math.random() * 100 + 1);
//guess variable to store the guessed number by user
var guess;
//output to store output to the user
var output;
//if the user guessed the number or not, initialize it to false
var guessed = false;
var elHints = document.getElementById('hints');

//do/while loop, while condition is based on if the user NOT guessing the number (e.g. guessed == false)
do {
  guess = prompt("Think of a number between 1 and 100, what is your number?");
  elHints.textContent += "You guessed the number " + guess + '\n';
  if (guess > number) {
    elHints.textContent += "You guessed too high, think smaller" + '\n';
    guessed = false;
  } else if (guess < number) {
    elHints.textContent += "You guessed too low, think bigger" + '\n';
    guessed = false;
  } else {
    alert("You guessed the right number!")
    guessed = true;
  }
}
while (guessed === false);

And here's the jsfiddle.

Community
  • 1
  • 1
Anthony
  • 1,439
  • 1
  • 19
  • 36