While we're fixing it, we can make the code even cleaner:
function accesSite(){
let ageLimite = prompt("How old are you ?");
let message = "Can you drive ? ";
if (ageLimite >= 16)
return message + " Ok, you can drive !";
return message + "No, you can't drive!" ;
}
alert(accesSite());
Note that I removed the else
block here:
- If the condition is
true
, then you hit a return statement
. Nothing below that return
can be executed.
- The other way around, if the condition is
false
, the first block will not be executed, the if
's return
will no be called, so the code below the if
is executed instead.
Or by using a ternary condition:
function accesSite(){
let ageLimite = prompt("How old are you ?");
return "Can you drive ? " + (ageLimite >= 16 ? " Ok, you can drive!" : "No, you can't drive!");
}
alert(accesSite());
I personally prefer keeping the prompt
result in a variable. Imo, that's more readable.