0

I'm building a guessing game that involves generating a number and a user visiting my page to guess it. The number is supposed to be from 1-5, and you should have only 3 tries to guess it.

So far, I've been able to get the user to be prompted, and to successfully generate a number. However, that's really about it. My main problems are:

  1. Getting the program to tell the user if it's an incorrect guess
  2. Having the amount of tries successfully displayed.

    <script type="text/javascript">
        var guesses = 3;//The number of guesses         
        alert ("I'm thinking of a number between 1 and 5. You've got 3 attempts to guess it");
        while ( guesses > 0 ) {
            var number = Math.random() * 5; //Generate a number between 1 and 5
            number = Math.ceil( number ); //Round it to 5
            var guess = prompt (" tries remaining. Go ahead!");
            guesses = guesses - 1;
            if (guess = number)
                alert ("You guessed the number!");
            else
                alert ("Unfortunate. Try again!");
        }
    </script>
    </head>
        <body>
            <script type="text/javascript">
                document.write ("The number was " + number);
            </script>
        </body>
    </html>
    

To add more to the second problem, you may have noticed that I had this line of code:

var guess = prompt (" tries remaining. Go ahead!");

However, when I do this:

var guess = prompt ( + guesses " tries remaining. Go ahead!");

The code won't run

Any help would be appreciated

P.S.
  • 15,970
  • 14
  • 62
  • 86
SkyjumperTalon
  • 101
  • 2
  • 10
  • 3
    A few typos I noticed: `=` is assignment, `==` and `===` are comparison. You have `if (guess = number)`. You're also not concatenating properly in `prompt ( + guesses " tries remaining. Go ahead!");`. Should be `prompt(guesses + " tries remaining. Go ahead!");` Learn how to check the browsers console for errors when debugging. – j08691 Sep 20 '17 at 22:44
  • 3
    `+ guesses " tries remaining. Go ahead!"` is a syntax error. JavaScript uses infix notation, not polish notation for operations (it seems you knew that for `guesses = guesses - 1;` at least). – Felix Kling Sep 20 '17 at 22:45
  • @j08691 I tested both == and === but none of them worked. However, this time, it's impossible to get it right. – SkyjumperTalon Sep 20 '17 at 22:48

2 Answers2

1

I am able to run your code. I pasted it into a snippet below.
The error I see is that I always guess the correct number, regardless of my guess.

I can even click Cancel, and it still thinks I guessed the correct number. Also, I never see my number of tries remaining. Also, you continue the loop even if I guess correctly.

<script type="text/javascript">
    var guesses = 3;//The number of guesses         
    alert ("I'm thinking of a number between 1 and 5. You've got 3 attempts to guess it");
    while ( guesses > 0 ) {
        var number = Math.random() * 5; //Generate a number between 1 and 5
        number = Math.ceil( number ); //Round it to 5
        var guess = prompt (" tries remaining. Go ahead!");
        guesses = guesses - 1;
        if (guess = number)
            alert ("You guessed the number!");
        else
            alert ("Unfortunate. Try again!");
    }
</script>
</head>
    <body>
        <script type="text/javascript">
            document.write ("The number was " + number);
        </script>
    </body>
</html>

The problem is in your logic... I've fixed that for you.
See my comments in the code, but summarized:

  • Wrong expression in your if statement
  • Assigning a new random number after each guess
  • Tries remaining not added to the string (concatenated)

 <script type="text/javascript">
        var guesses = 3;//The number of guesses         
        // ##CHANGED## 
        // Pulling the number outside the loop - because otherwise you are generating 
        // a new random number after each guess.
        var number = Math.random() * 5; //Generate a number between 1 and 5
        number = Math.ceil( number ); //Round it to 5
        // ##
        alert ("I'm thinking of a number between 1 and 5. You've got 3 attempts to guess it");
        while ( guesses > 0 ) {
            //## CHANGED - concatenate guesses counter to prompt string   
            var guess = prompt (guesses + " tries remaining. Go ahead!");
            guesses = guesses - 1;
            
            //## CHANGED CONDITION INSIDE IF BELOW ##
            // Previously you were assigning a variable with =
            // == would do an implicit type conversion, which is what we want
            // thus typing in "2" as your string gets converted to the number 2
            
            if (guess == number) {
                alert ("You guessed the number!");
                guesses = 0; // ## HACKY BUT EASY WAY TO EXIT THE LOOP ONCE DONE
             }
            else {
                alert ("Unfortunate. Try again!");
            }
        }
    </script>
    </head>
        <body>
            <script type="text/javascript">
                document.write ("The number was " + number);
            </script>
        </body>
    </html>
  • My question is, after the user gets the number right, how do I prevent the system from still giving it more attempts? Say I got it first try, the program still gives second and third attempts to me, and says I didn't get the number – SkyjumperTalon Sep 20 '17 at 23:11
  • @SkyjumperTalon The condition to keep prompting that you specified is that `guesses > 0`. I set `guesses = 0` so that your loop condition will exit after the number is guessed correctly. – TinkerTenorSoftwareGuy Sep 20 '17 at 23:47
  • @SkyjumperTalon If you don't like that hacky solution, you could set a boolean variable, like `var correct = false` before loop, and make your while condition be `while (guesses > 0 && !correct)`. I was just being lazy. – TinkerTenorSoftwareGuy Sep 20 '17 at 23:49
  • @SkyjumperTalon Also, you'd have to remember to set `correct = true` when `guess == number` instead of my hacky `guesses = 0`. Fairly simple, and clearer. – TinkerTenorSoftwareGuy Sep 20 '17 at 23:50
-1

Your first problem looks to be because instead of doing a comparison, you are doing an assignment. This will compare the two numbers:

if (guess === number)

For more info on comparisons and why you should almost always use === instead of ==, take a look here: https://stackoverflow.com/a/359509

Now, as for your second problem, you are placing the + before guesses instead of after. Think of this as "adding strings" together. Here's what that would look like:

var guess = prompt (guesses + " tries remaining. Go ahead!");
rikil
  • 23
  • 5