Control Flow Question


#1

https://www.codecademy.com/courses/learn-javascript/lessons/control-flow/exercises/comparison-operators?action=lesson_resume

Not sure why it is outputting both strings. Also, if anyone can explain the 'else' statement needs a semi-colon after the parenthesis, and t\not the 'if' statement, I'd appreciate it.

Error on line 4: Expected an assignment or function call and instead saw an expression

var hungerLevel = 10;
if (hungerLevel > 7)  {
  console.log("Time to eat!");
} else (hungerLevel < 7); {
  console.log("Lets\'s eat later.");
}

#2

Hi @webslayer99973,

if/else statements should follow this syntax:

if (condition1) {
    // do something, ie: console.log("some string");
}
else {
    //do something else, ie: console.log("another string");
}

You only need to state what is the if condition, the rest that do not fall into the if condition, will fall into else.

Hope it helps :slight_smile:


About semicolon in your code on this exercise:
I guessed you have inserted a semicolon probably due to this situation like another user:

If you have the time, do go through the answers as I believe @ghostlovescore and I spend some time in answering the other user's questions. I hope it will contribute towards your understanding.


12. Functions & if / else
#3

Ahh, thank you. It does :slight_smile:

I somewhat figured this out in a later lesson, when trying to run if/then statements using Boolean conditions, but setting my variable to something random trying to get it to run else, when it would only accept true/false


#4

You're welcome. Yeap, using Boolean could be little obvious.

if (true) {
    // do something;
}
else {
    //do anything if condition is false ;
}

:wink:


#5

I'm sorry I'm also having an issue with this lesson-

I am thought all I had to do was change the var hungerLevel from 5 to 10 but it keeps telling me I am wrong and will not let me continue... if anyone could help it would be a big help! :grin:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.