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)) {
        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.

1 Like

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
And the next one (decreasing to the next smaller representable value)
If one prints out some result from Math.random
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?


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!


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

Any feedback is welcome, and will be seriously taken!

Hi, I don’t know why my code doesn’t work. Can somebody help me check it out?

If you expand “does not work” into what it’s doing differently from what you meant then you’ve got a lead to follow.

I entered a number and then clicked “make a guess”, but the game didn’t start. I still don’t know why.

What part of your code should have ran as a result? Did it? What input did it receive? What output did it produce?

If you consider what should happen then you’ve got plenty of things that you can make observations about, things you can look at to find out if they are as you expect.

Hi, I tried debugging my code and I don’t know where I went wrong.

The number guesser just won’t work in the codeacademy editor.

Have you tried using the console to see if you get any errors? If the lesson doesn’t have a console, use the browser used in console (f12 is the common keyboard shortcut)