-2

Hello I'm new to javascript and trying to run the following

var age = prompt("What is your age");
if (age < 19) {
  document.write("You can't drink");
}
else if (age === 19) {
  document.write("you can finally drink");
}
else (age > 19) {
  document.write("you can already drink");
}

I can't seem to get anything to show up but if I change my code to remove the else statement and change the === to just == my code runs. This is how it looks like when it runs after I get rid of the else statement and === operator.

var age = prompt("What is your age");
if (age < 19) {
  document.write("You can't drink");
}    
else if (age == 19) {
  document.write("you can finally drink");
}

I'm trying to get the first block of code to run but can't. The page is empty and no prompt shows up. Someone please help.

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
  • 3
    `else (age > 19)` doesnt make much sense, else is when all if and else if are false, you dont need any condition. – Chris Li Sep 27 '18 at 01:22
  • 1
    There is no condition in an else branch, it's simply ignored. See [*The if Statement*](http://ecma-international.org/ecma-262/9.0/#sec-if-statement), the (simplified) form is: `if (expression) { statement } else { statement }`. – RobG Sep 27 '18 at 01:25

4 Answers4

1

There are a few mistakes in your code:

1) prompt method return string no number, so :

Use == instead of === :

else if (age == 19) {

PS: (19 == '19'); is true but (19 === '19'); is false

OR convert age to number :

else if (Number(age) === 19) {

2) You should not use condition for else , so you must change else like this:

else { document.write("you can already drink"); }

var age = prompt("What is your age");

if (age < 19) { document.write("You can't drink"); }

else if (Number(age) === 19) { document.write("you can finally drink"); }

else { document.write("you can already drink"); }
halfer
  • 19,824
  • 17
  • 99
  • 186
Ehsan
  • 12,655
  • 3
  • 25
  • 44
0

Your problem is the last else statement. You are putting a condition after the else, which does not work. You only add the condition after another if. You can fix this by removing the condition like so:

var age = prompt("What is your age");
if (age < 19) {
  document.write("You can't drink");
}
else if (age === 19) {
  document.write("you can finally drink");
}
else {
  document.write("you can already drink");
}
2pacalypse
  • 65
  • 2
0

Firstly you need to understant differnce between "==" and "===" . From this you can conclude

JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and: Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions. Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another. Two Boolean operands are strictly equal if both are true or both are false. Two objects are strictly equal if they refer to the same Object. Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]

And secondly "else" with a condition is always written as "else if" ...else is block which is runs when none of its above conditions is true i.e none is true ..read from here for more information

p u
  • 1,395
  • 1
  • 17
  • 30
0

You seem to be having some logical error in your last else statement. If none of the first 2 conditions are true, then logically speaking, the last condition must be true. else does not hold any conditions. It's simply just else in terms of the previous condition(s). Else you would have to use else if() again, but as mentioned earlier, in case neither of the conditions are true, then the last condition must be the case regardless in this example. Therefore, just else makes the most sense.

Example:

if (age < 19) {
  document.write("You can't drink");
}
else if (age === 19) {
  document.write("you can finally drink");
}
else {
  document.write("you can already drink");
}

Another thing in terms of your operators. == means equal to, and can be used to compare different types. I.e. you can compare a number type to a string type, and if they both hold the same value, the statement will be true. However, === is a strict equals, and means that the things being compared must be the same value AND the same type.

Example:

var x = 5; //number
var y = '5'; //string

if(x == y) {
    //true, because they are equal, both has the value 5
}

if(x === y) {
    //false, because even though they are equal in value, they are not the same type
}

var a = 8; //number
var b = 8; //number

if(a === b) {
    //true, because they are both equal, and the same type
}

So, to clearify,

== checks if the values are equal, no matter the type.

=== checks if the values are equal AND are of the same type.

More simple documentation on operators here.

Martin
  • 2,326
  • 1
  • 12
  • 22