1
var check = true;
var number = Math.floor(Math.random() * 20);

while (check === true){
var guess = prompt("I picked a number 0 to 20, try to guess it!");
if (number === guess) {
    print("You guessed correctly! Good job!"); 
    check = false;
}
else if (number < guess) {
    print("\n\You guessed too high!"); 
}
else if (number > guess) {
    print("\n\You guessed too low!"); 
}
else {
    print("\n\Error. You did not type a valid number");
    exit(); 
}
    print("\n\Guess: " + guess + ".");
}  

When I try running this program, I get all the way up to the correct answer, but it doesn't work! Even if the randomly generated number is 13, and I guessed 13, it would go through and it would say it is invalid.

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
MkayBye
  • 25
  • 4
  • `print`, `exit()`? – j08691 Jul 18 '17 at 20:14
  • 1
    I have seen those methods before. Don't remember the name it was in a high school they had a super simple runtime environment set up for javascript so learners didn't have to deal with the complex environment of a browser or node.js. – Thomas Devries Jul 18 '17 at 20:16
  • I use the print command because the website I use uses print instead of console.log – MkayBye Jul 18 '17 at 20:23

5 Answers5

2

Your guess is a string. It is the text entered by the user you need to convert it into a number in order to be able compare it with your guess so replace

var guess = prompt("I picked a number 0 to 20, try to guess it!");

with

var guess = Number(prompt("I picked a number 0 to 20, try to guess it!");

This will turn your guess from your user into a number or a special value NaN if it isn't formatted correctly.

You could also use the == operator which will automatically convert between types. I would recommend against using the operator if you are new to javascript as it can have some confusing and unexpected behaviors.

Thomas Devries
  • 839
  • 4
  • 18
  • Shorthand for `Number(someValue)` is `+someValue`, it's called the [unary plus operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_()) – Patrick Roberts Jul 18 '17 at 20:16
  • This is true. However it is less readable in my opinion. Edit: Less readable to someone without javascript experience. – Thomas Devries Jul 18 '17 at 20:18
1

You are comparing the return value of prompt (a string) with the return value of Math.floor (a number).

Since you are using === and they are different data types, you'll never get a match.

Use == or explicitly cast the number to a string or vice versa.

number === +guess
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • A couple points I think could improve your answer: 1) mention what `+` is 2) probably more prudent to cast the value when being assigned to `guess` rather than when being used in the strict equality test. – Patrick Roberts Jul 18 '17 at 20:19
1

=== is strictly typed so it will not compare the int to a string.

Convert guess to an integer. You should first validate it though in case the user inputs something other than an int.

var guessInt = +guess; // the plus converts to an integer
if(isNaN(guessInt)) 
    continue;
if (number === guessInt) {
    print("You guessed correctly! Good job!"); 
    check = false;
}
ThomasK
  • 300
  • 1
  • 8
0

You are using the triple = operator which checks for type equalness too. when you compare the prompt value (your guess variable) to your number variable. you are comparing a String and a Number. To make this work you could use

number == guess

or

Number(guess)

caxco93
  • 33
  • 5
0

You’re using strict equality comparison, which also compares types. prompt returns string values. Use parseInt to cast to a number.

var guess = prompt("I picked a number 0 to 20, try to guess it!");
guess = parseInt(guess, 10);

The second parameter tells the number base (10 is for decimal, 16 for hexadecimal, etc.). In non strict mode (aka sloppy mode) you may experience accidental conversion to octal (base 8) when parsing strings with a leading zero. Always specify the base to avoid this.

You might want to learn more about JavaScript strict mode.

Watilin
  • 286
  • 3
  • 9
  • I think you meant to say "you may _experience_ accidental _base conversion to_ octal". Also, no one calls it "sloppy mode", it's simply "non-strict mode". You may also like to know that the ECMAScript 6 edition specification updated the definition of `parseInt()` to default to base 10 by default for _all_ input, so that is no longer an issue except in non-compliant browsers like older versions of Internet Explorer. – Patrick Roberts Jul 18 '17 at 20:24
  • Sorry for mingling “experiment” with “experience”. I edited my answer. – Watilin Jul 18 '17 at 20:33