0

The following code is not quite working.

Here is a couple of my observations:
1- No matter what the user guesses, the message "You Win!" Always show up
2- Console.log(yourguess) shows a blank space

Code:

HTML

<!DOCTYPE html>
<html>

<head>
    <title>Lab12</title>
    <style>

    </style>
</head>

<body>
    <p>Guess a number between 1 and 1000:</p>
    <input type="text" id="theguess"> <br>
    <button type="button" onclick="calc()" id="thebutton">Check Guess</button>
    <p id="clue">No Guess Yet</p>
    <p>You guessed</p>
    <p id="c"> 0 </p>
    <p>Times!</p>
    <br>
</body>
</html>

Javascript:

    <script>
        let correct = Math.floor(Math.random() * 1000);
        let yourguess = document.getElementById("theguess").value;
        let count = 0;

        calc = () => {
            console.log(yourguess)
            if (thebutton) {
                count++
                document.getElementById('c').innerHTML = count;
            }
            if (count === 10) {
                document.getElementById("thebutton").disabled = true;
            }
            if (this.correct === this.yourguess) {
                document.getElementById('clue').innerHTML = 'You Win!';
            }
            else if (this.correct < this.yourguess) {
                    document.getElementById('clue').innerHTML = 'Too low. Try Higher!';
                }
            else{
                    document.getElementById('clue').innerHTML = 'Too High. Try Lower!';
                }

        }
    </script>

There is no syntax error, which is why I can't quite point my finger on where the error is

Stackoverflow people, do your thing!

3
  • 1
    You are getting the value of your guess outside your function. Hence the value is the input box value when the page first loaded. Commented Nov 17, 2019 at 18:51
  • Also you never defined or assigned a value to thebutton variable. Commented Nov 17, 2019 at 18:53
  • Okay. The blank (yourguess) is solved. But how to fix the (You Win!) Commented Nov 17, 2019 at 18:57

3 Answers 3

1

yourguess is assigned when the page loads... when it’s blank. Variables in script tags are assigned right away.

To fix this, get the value of yourguess as part of your calc routine... AFTER User inputs their guess.

For extra credit, do some input validation; make sure that the guess is a number/integer, and falls within the range.

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

Comments

1

if you did the following

console.log(this.correct, this.yourguess);

You would get "undefined, undefined" which is why your guess is always correct by the following if statement

if (this.correct === this.yourguess) {

The solution is to remove "this."

Bear in mind, correct is type number and yourguess is a type string, when doing === it won't ever be correct due to the type being different. The following should solve this issue.

if (correct === parseInt(yourguess, 10)) {

Your other logic for too low / high is wrong for similar reasons.

Finally,

let yourguess = document.getElementById("theguess").value;

needs to be within the calc function given you need to update this value each time you click the guess button.

Comments

1

There's a few errors here.

  1. You are reading the input value outside the calc() function, this means that it will always read empty (default input value).
  2. Same applies for the random number. You generate it only once the page starts. This means that the number will always be the same.
  3. You are using Math.floor() instead of Math.round(). This means that the latest number (1000) will almost never occurs. (999.9999999999999) will turn into 999. Using round, anything greater then 999.5 will be 1000.
  4. What is thebutton variable? You never initialized it.
  5. You are using this. before some variables. Remove it.

So you code should be something like this:

    <script>
        let count = 0;

        calc = () => {
            let correct = Math.round(Math.random() * 1000);
            let yourguess = document.getElementById("theguess").value;

            console.log(yourguess)
            if (thebutton) { // what is thebutton? What's you expectation from this if?
                count++
                document.getElementById('c').innerHTML = count;
            }
            if (count === 10) {
                document.getElementById("thebutton").disabled = true;
            }
            if (this.correct === yourguess) {
                document.getElementById('clue').innerHTML = 'You Win!';
            }
            else if (correct < yourguess) {
                    document.getElementById('clue').innerHTML = 'Too low. Try Higher!';
                }
            else{
                    document.getElementById('clue').innerHTML = 'Too High. Try Lower!';
                }

        }
    </script>

1 Comment

thebutton is the id of the onclick, I'm using it to disable the button once the user tries 10 time. Thanks for helping. But The user still always wins no matter what the input is. Edit: acutally I'm stupid. I just replaced "thebutton" with "true" and it worked the same way.

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.