FAQ: Conditional Statements - Else If Statements

Just copied and pasted the solution and got the same thing.

Same here, I did the exercise 5 times before giving up and getting the answer. My code was an exact match to the answer given. I even copy and pasted the answer given after restarting the exercise and was told it is incorrect. Bug

you’re missing the else statement at the end?


Is there any difference between using
if summer…
else if winter…


if summer…
if winter…

What’s the interest / benefit of using the else if instead of a simple if ?

Thank you

Use else if when appropriate, this makes your code easier to understand and read (for another developer as well). when using multiple if's and another developer looks at your code, they might assume multiple condition can be true.

OK I understand, thank you :slight_smile:

hi I skipped this but got the info but I was so hard to understand what I did wrong I spent 15mins going to the solution and back so for everyone else please fix this thank you cause I know that u understand but it doesn’t be clear have a nice day

If you would like to try to figure out what was wrong with your code I would love to help. Not sure if you are too far past this to care (since it has been over a month since you posted), but if you want to come back to it and reply with your code we can figure it out.

thank you but I understand now

I’m going through the Else if statements and every time I write out code, I’m getting the correct answer on the right, but the program prompts me as wrong. In most cases the only difference is I don’t have the spaces as the program. (For example: else if (season === ‘fall’) {
console.log(“It’s fall! Leaves are falling!”); } vs else if (season===‘fall’){
console.log(“It’s fall! Leaves are falling!”);)} Are the spaces making that big of a deal or it’s just how this program wants it typed out?

One way that your code is checked is to compare that your log is identical to what the Codecademy checker expects your log to be.

Ok, I’m now on the Magic 8 Ball assignment. The first time I did it, I did it with Switch Statements and it sorta worked. I watched the “Get help” video and found where I inverted a couple things. So, I cleared it all and started over using if…else if…else statements. I did better, but for some reason, my eightball answers always stay the same. I added a console.log to see if the Math Methods were correct (and the number was randomly changing) but it still keeps coming back “It is decidedly so”. What am I doing wrong?

let userName = ‘Nancy’;
if (userName){
console.log (Welcome, ${userName}!);
} else {
console.log (‘Hello!’);

let userQuestion = ‘Does Billy love me?’;
console.log(Question: ${userQuestion});

let randomNumber = Math.floor(Math.random() *8);
console.log (randomNumber)
let eightBall = ‘’;

if (randomNumber = 0) {
eightBall = “It is certain!”;
} else if
(randomNumber = 1) {
eightBall = “It is decidedly so!”;
} else if
(randomNumber = 2) {
eightBall = ‘Reply hazy. Try again!’;
} else if
(randomNumber = 3) {
eightBall = ‘Cannot predict now!’;
} else if
(randomNumber = 4) {
eightBall = ‘Do not count on it!’;
} else if
(randomNumber = 5) {
eightBall = ‘My sources say no!’;
} else if
(randomNumber = 6) {
eightBall = ‘Outlook not so good’;
} else {
eightBall = ‘Signs point to yes!’;
console.log(The eight ball answered: ${eightBall});

like you said, the random number is correctly generated. So then the next logical place to look for problems are the conditions itself.

= is assignment, why would you want to use assignment here? why not use a comparisons operator? to see if the values are equal.

I believe the best way to learn coding is to code itself.
So please type about:blank in a new tab, ctrl+shift+j, and type this into the console: (please type it not just copy 'n paste)

console.log('It's sunny out there, don't you think?');

This statement will give you an argument list error as the console assumes that there 2 arguments because there are 2 strings ('It' and 't you think?') and there are nothing seperate apart from 5 undefined variables that are separated by spaces.

console.log('It\'s sunny out there, don\'t you think?);

This is a perfectly valid piece of code as the console acknowledges that there are apostrophes in the string that do not mark the end of a previous string and are in fact part of the string itself.

Hope this helped you on your coding journey!

When I type this statement with a (.) why it is incorrect? :point_down:
‘It’s sunny and warm because it’s summer!’);

I’m not sure where (.) is in your code, but there’s another issue with it.

Single or double quotes surround strings. This means that the apostrophe in It's is interpreted as marking the end of the string. This throws an error.

console.log('It's noon.'); // throws an error

One way to include apostrophes or double quotes in your strings is to use escape notation. \ is the escape character.

console.log('It\'s noon.'); // prints It's noon.

More on strings and escape notation here. Welcome to the forums!

1 Like