If else chains Basic JS

I am confused about how to go about doing this. Basic JavaScript: Chaining If else statements

What specially are you confused about? The examples? The instructions? What code have you tried so far to solve the challenge?

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch/

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

https://www.w3schools.com/js/js_if_else.asp

The type of confusion I am having involves my belief which is this:
In order for you to completely understand something (like languages) you have to be able to read it and get it then be able to spit it right back out understanding what you are saying.

I am having trouble spitting it right back out.

I would say try and explain it here to us right now, that way you can rework your understanding of it with some help from the forum.

Like tell me in your own words how you chain if/else statements and why you would do that in the first place.

if i don’t go to movies at 16:00 today
else if i will go to the city at 20:00
else i don’t do them either I will stay at home

if (time < 10) {
  i go to = "movie";
} else if (time < 20) {
  i go to = "city";
} else {
  i go to = "My home";
}

yes good idea :3 ra why don’t you try it

I just realized that I do not know how to chain the if/else statements together. I think I understand the English translation of the JavaScript that KittyKora put here:

If you weren’t going to the movies at that time
You would go to the city at another time
If you do neither of them, you will stay at home

Is the chain the code?

I get what you’re trying to show, but “go to” isn’t anything. You should give an example with proper syntax like:

function checkAge(age){

    if (age < 18) {
    return "Sorry, you can't enter";
    }

    else if (age >= 18 && age < 21){
    return "You can enter, but you can't drink.";
    }

    else {
    return "You can enter and drink";
    }

}

console.log(checkAge(19));

// with the 19 passed into the function, you'd get "You can enter, but you can't drink"

check my example above :smiley:

Basically we’re building a function that checks the number provided for age (in this case 19).

IF the age provided is less than 18, this person gets bounced, they can’t come in.

Else IF the person is over 18 but younger than 21, they can come in, but they can’t drink alcohol (hahah going by the US age limits)

Else neither of the above statements pass as true, so therefor this person must be at least 21 or over, so they can come in and drink

Here’s an example with strings:

function gateKeeper(password){

const correctPass = 'apples';

    if (password === correctPass) {
    return "You may enter!";
    }

    else if (password === ''){
    return "Say the password.";
    }

    else {
    return "That is the wrong password!";
    }

}

console.log(gateKeeper('oranges')); // That is the wrong password!
console.log(gateKeeper('')); // Say the password
console.log(gateKeeper('apples')); // You may enter!

I think I understand: Would this be it? (answering the lesson as well)

if ( num < 4) {
return "Tiny";
}
else if ( num < 8) {
return "Small";
}
else if (num < 14 ) {
return "Medium";
}
else  {
return "Large";
}

For “complex logic”, is this correct?

Perfect! (Granted I’d have to see what the challenge is calling for) but just running this code with different numbers returns the strings you set up.

Keep in mind too, if you end up needing a ton of conditional statements (like more than you have here), you might want to consider using a Switch statement instead.

You might have to see the lesson because it is asking for at least 4 else statements, at least 4 if statements, and at least one return statement.

hmmm yeah, could you post a link to the challenge?

Ah I found it. Okay it looks like you’re not using the proper values for your conditionals, ie:

num < 5 - return “Tiny”
num < 10 - return “Small”
num < 15 - return “Medium”
num < 20 - return “Large”
num >= 20 - return “Huge”

Take what you’ve got going there and add “else if” statements for each of those between the sizes of Tine and Huge (those two meanwhile are gonna be if and else respectively).

If you’re still getting stuck after that, post what you have and I’ll take a look at it