1

I am writing a game and I have this problem: the last if statement (and all its else if statements) are never executed.

Here is the code that doesn’t work:

const compare = prompt("1.mars, 2.jupiter, 3.moon")

if (compare === 2) {
  confirm("your airplane crashed and you died")
} else if (compare === 1) {
  confirm("you arrived safely")
} else if (compare === 3) {
  confirm("you survived an airplane crash but you need to escape")
}
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
crazyvlad
  • 11
  • 1
  • 2
    ``prompt`` queries a *string*, but you compare with *integer*s (e.g.: ``'1' === 1`` is equivalent to ``false``) – Binkan Salaryman Jul 06 '15 at 08:02
  • you could change your if statements to just use `==` instead of `===`, therefore the type doesn't have to match too: `if (compare == 2)` ... etc – duncan Jul 06 '15 at 08:09

5 Answers5

1

As @Binkan Salaryman very accurately pointed out, prompt is returning a String ('1', '2', etc.).

Either use == to compare untyped values like compare==2 or compare with the correct type: e.g. compare==='2'

SDekov
  • 9,276
  • 1
  • 20
  • 50
  • `==` and `!=` are almost [never recommended](https://stackoverflow.com/q/359494/4642212). They’re not always intuitively transitive since they oftentimes unnecessarily perform type coercion. Always use `===` and `!==` instead. Either use `Number(prompt(`…`))` or compare against strings. – Sebastian Simon Apr 05 '21 at 07:25
0

use Equality operator( == ) instead of Strict equality ( === ) operator. With ===, comparison returns true only if both the operands same in type as well. In your case, the result of prompt returns string, and

compareInt === 2; // '2' === 2 

returns false as you are checking for string and number to be same.

'2' === 2 returns false as type checking is also done
'2' == 2 returns true as no type checking
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • _“as no type checking”_ is wrong. `==` and `!=` are almost [never recommended](https://stackoverflow.com/q/359494/4642212). They’re not always intuitively transitive since they oftentimes unnecessarily perform type coercion. Always use `===` and `!==` instead. Either use `Number(prompt(`…`))` or compare against strings. – Sebastian Simon Apr 05 '21 at 07:28
0

I made a little demo. Refactored your code a little. Demo

The main problem as some people have pointed out is that prompt returns a string even if you write a number.

You'll need to convert this string to an int using parseInt() function.

if(jack === 'yes'){
    var compare = parseInt(prompt("1.mars, 2.jupiter, 3.moon"));
    switch(compare){
        case 2: confirm("your airplane crashed and you died")
            break;
        case 1: confirm("you arrived safely")
            break;
        case 3: confirm("you survived an airplane crash but you need to escape")
            break;
        default:
            confirm("An error occured");
    }
}
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
  • Please use `parseInt` [_with_ the second parameter, `10`](https://stackoverflow.com/q/16880327/4642212). Consider using [`Number`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#Function_syntax) or [`parseFloat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) instead. – Sebastian Simon Apr 05 '21 at 07:27
-1

Try:

compareInt = parseInt(compare)

  Final code looks like:

var compare = prompt ("1.mars,2.jupiter,3.moon")
var compareInt = parseInt(compare);
if (compareInt===2) {confirm ("your airplane crashed and you died")}
else if (compareInt===1) {confirm ("you arrived safely")}
else if (compareInt===3){
confirm("you survived an airplane crash but you need to escape")} 
-1

for work your code you don't need to strict check by === you can use the simple comparison with ==

  • because === will compare both's type also as well as the value
  • while == only compares the value
R3tep
  • 12,512
  • 10
  • 48
  • 75
Himesh Aadeshara
  • 2,114
  • 16
  • 26