0

I am learning JS. So i'm trying to create a function but the "else" part is not working. Can you help me to understand why?

function accesSite() {
    let ageLimite = prompt("How old are you ?");
    if (ageLimite >= 16) {
        var yes = " Ok, you can drive ! ";
    } else {
        var no = "No, you can't drive!";
    }
    let message = "Can you drive ?" + yes || no;
    return message
}

alert(accesSite());
Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
Gabriel
  • 56
  • 8

4 Answers4

8

You can do something like below. Declare a variable and then change it accordingly:

 function accesSite(){
     let ageLimite = prompt("How old are you ?") >= 16;
     //create variable
     var canDrive = "";
     if (ageLimite >= 16){
         //change it accordingly
         canDrive = " Ok, you can drive ! " ;
     } else {
         //change it accordingly
         canDrive = "No, you can't drive!" ;
     }
     let message =  "Can you drive ? " +  canDrive ;
     return message
}

alert(accesSite());

A better version in a single line:

function accesSite(){
    return "Can you drive ? " + ( prompt("How old are you ?") >= 16 ? " Ok, you can drive ! " : "No, you can't drive!");
}

alert(accesSite());
Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
3

You could use parenthesis for the logical OR ||, because the plus has a higher operator precedence as the logical OR.

let message =  "Can you drive? " + (yes || no);
//                                 ^^^^^^^^^^^ evaluate first

A better approach would be to declare all constants and variables on top and return with the result directly without taking another variable for the return string.

The function feature the early exit paradigm, that means if you need to return from a function, you could organize the condition/s in the way that you do not need else parts, because you use return for terminating the function at early stage.

Sources:

function accesSite(){
    const canYouDrive = "Can you drive? ";
    var ageLimite = prompt("How old are you?");

    if (ageLimite >= 16){
        return canYouDrive + "Ok, you can drive!";
    } 
    return canYouDrive + "No, you can't drive!" ;
}

alert(accesSite());
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 6
    This answer solves the problem but not the bad practice. Scoping of the variable is the issue here: OP is creating 2 variables for 1 purpose (appending it to the final message). – Abbas Jan 15 '18 at 12:35
  • @NinaScholz I must have still been typing my comment while you added this to your answer afterwards, but you're right. :) – Abbas Jan 15 '18 at 12:37
  • 4
    " _Beside that, i suggest to use only one variable for the result string and declare all variables at top of the function._ ". Well, _do_ that when you write your answer... – KarelG Jan 15 '18 at 12:57
  • 4
    I'm at a loss for words why this _"bandage"_ is getting this many votes. It's not fixing any of the glaring issues in the question. It's just making the OP's code work at all costs... I'd expect more from a high rep user that is meant to serve as an __example.__ – Cerbrus Jan 15 '18 at 12:58
  • 2
    My comment isn't rude. It's blunt, at most. The only reason I added my answer, is because there wasn't a __proper__ answer here, yet. Only bandages. – Cerbrus Jan 15 '18 at 13:13
3

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.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • Wow! but i dont understand how it's possible to use the line "no" without an "else" in your exemple. – Gabriel Jan 15 '18 at 12:43
  • 1
    @Gabriel: If the condition is `true`, then you hit a `return` statement. Nothing below that can be executed. The other way around, if the condition is `false`, the first block will not be executed, the `return` will no be called, so the code below the `if` is executed instead. – Cerbrus Jan 15 '18 at 12:47
  • @Nick: If the user closes the prompt, the return value will be "falsy", any way. – Cerbrus Jan 15 '18 at 13:34
1
function accesSite() {
    let ageLimite = prompt("How old are you ?");
    var msg = "Can you drive ? ";
    if (ageLimite >= 16) {
        msg = msg  + " Ok, you can drive ! ";
    } else {
        msg = msg  + "No, you can't drive!";
    }
    let message = msg;
    return message
}

alert(accesSite());
jasbir
  • 216
  • 1
  • 8