Number Guesser Challenge Project (JavaScript)

it does, what possible values could return Math.floor(Math.random() * 9); give? and what possible values could return Math.floor(Math.random() * 10); give?

you could add console.log() to each condition:

if (Math.abs(human === computer)) {
        console.log('a');
        return true;
    } 

and then 'b' to next condition, and so forth.

This way, you know which condition causes you to keep winning. Once you have figured this out, you can tear apart why this condition/comparison keeps evaluating to true, and thus why you keep winning.

1 Like

I don’t know how many decimals Math.random gives, but assuming, for instance, it gives two:

Multiplied by 10, it could give any number between 0 and 9.9. After Math.floor, we’d get one integer between 0 and 9 (included).

Multiplied by 9, it could give any number between 0 and 9.81. After Math.floor, we’d get one integer between 0 and 9 (included).

I think those would be the values, so it wouldn’t change much. However, I’m not sure if the likelihood of getting a 9 would be the same (because of the decimals) if we multiply by 9 or if we multiply by 10.

you could simply log Math.random to (roughly) see how many decimals yo uget

Or you made a typo here, or please check your math. 0.99 * 9 is not 9.81.

the difference between 9 and 10 is one, how can the result of the multiplication have a difference in value of 0.09?

Or you made a typo here, or please check your math. 0.99 * 9 is not 9.81.

My bad. I miscalculated it.

0.99 * 9 = 8.91

After this, applying Math.floor, could we ever get a 9?

I think no matter how many decimals we add, 9 multiplied by a number lower than 1 will be lower than 9. So, we won’t get the value 9 in any scenario. Am I right?

given Math floor floors (round down) the number, we can’t get 9

so using 9 or 10 in our multiplication makes a difference in the result of our program.

yes, you are right.

The largest number Math.random gives is probably
0.99999999999999988897769753748434595763683319091796875
And the next one (decreasing to the next smaller representable value)
0.9999999999999997779553950749686919152736663818359375
If one prints out some result from Math.random
0.34509011895704167
Then yeah that’s rounded to about where the steps happen.

Ok. Thanks for the correction.

Hey there @elogram!

I had the same problem with the alert message as you did and worked for several hours on a solution.

To display the alert() message, you will actually need to go into the game.js file. script.js only deals with scores in the game, not the user’s input.

Notice in game.js on lines 16-20 it says this:

guessButton.addEventListener('click', () => {
  // Generate the target value
  target = generateTarget();
  // Retrieve the player's guess
    const currentHumanGuess = humanGuessInput.value;

This line of code starts all the logic for the guessing button. And here, as the comment mentions, it creates a variable (currentHumanGuess) that is assigned to the value inputed by the user (humanGuessInput.value).

Now that we know which variable is assigned to the user’s input (currentHumanGuess), we’ll need to create some logic to, as the project says, “check whether the user guess is between 0 and 9 and alert() the user that their number is out of range.”

Thus on line 21 of game.js, create an if-statement with an OR operator that alerts the user of the correct numerical range:

if (currentHumanGuess < 0 || currentHumanGuess > 9) {
    alert('Please enter a value between 0 and 9.');
  }

Reload your index.html. Type in a number less than zero or greater than 9. And voilà: a window alert appears preventing you from continuing the game without the write numerical value range.

Does this all make sense?


https://sweetleaf9.github.io/Number-guesser/

Was able to get the game working properly with out any issues that I can notice.

This is my code i modified based on few suggestions, still doesn’t work.

Here’s my code!
Enjoyed it a lot!
I’ve also made it (accidentaly but happily) so that when the user guesses out of range, they lose…

Any feedback is welcome, and will be seriously taken!
Thanks!

Completed!

Had some struggle with alerting, but now it’s all past…

Any feedback is welcome, and will be seriously taken!