Curious about notifications in the editor


#1

A lot of times when doing the exercises, i'll write my code, and it will run fine, but in the editor i get red dot notifications. I'll give you an example

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

Now, on line 5, the beginning of the else i have a red dot notification that reads "expected an assignment or function call and instead saw an expression" "missing semicolon"

Now i'm just starting out so I certainly don't know the finer points of coding JS. What i'm wondering is the significance of it here, is my code sloppy? What needs to be done?

I remember in the HTML course that sometimes the editor will basically still run code sometimes even if it isn't written exactly right, Is that the case here?

I want to make sure I'm doing things as correctly as possible. Thank you for any insight anyone can offer me.

`


More on Control Flow in JS - 11 - And
Control Flow Question
#2

And that's great, and also the reason why the red dot appears.

Say you're typing console.log("Time to eat!") ... and you've forgotten the semicolon. You'll get a red dot there.
The red dot just signals hey careful pal, you've forgotten something, check it out. It's there to help you.

It's easy to make silly mistakes in JS. ■■■■ some people categorized it the worst language as it will let you do things that simply don't make any sense whatsoever, but let you get away with it. Doesn't mean it's right though.

So, learn best practices from the start, mind the red dot, and get familiar with the syntax. By the end of the course you should be alright and ready to move towards deeper waters.

Edit: funny that my word up there got redacted. The word was the opposite of heaven. How random.


#3

var moonPhase= "full";
if (moonPhase === "full") {
  console.log("Howwwlll!!");
}
else { (moonPhase !== "full");
      console.log("I swear I am not a werewolf...");
     }

In the above example I set a semicolon with the else statement, and yes that removed part of the notification, a little confused as to why the else statement needed the semicolon but no the if statement, and it will run regardless buuut, still one notification I can't get rid of "expected an assignment or function call and instead saw an expression". Again, the code runs, but I'm not sure what this means and its bugging me.


#4

That code is incorrect, though (and yet it does let you pass, that's how lenient JS is).

You need to analyze the if / else statement first.

If the condition is true, then it's true.
Else, the condition is false.

You don't need to specify that moonPhase isn't full, because it's by default the opposite of moonPhase is full.

Besides the logic aspect of the if / else statement, the syntax is incorrect.

You could technically have used 'moonPhase isn't full' with else if.

if moonPhase is full, 
then Howwwlll
else if moonPhase isn't full,
then I swear I'm not a werewolf
else 
then nothing happens, I'm just a regular human being

You would have written the else if like this:

else if (moonPhase !== "full") {
      console.log("I swear I am not a werewolf...");
}

So that was a good example you provided, as to how lenient JS actually is. And it confirmed my previous allegation that it will let you get away with mistakes or bad syntax, even dubious logic sometimes (I'm not pointing at you, but rather adding strings to numbers and other silly things that give you actual silly results, anyways...) .

As I said, get the syntax straight and you'll do just fine :wink:


#5

I really appreciate the feed back, much appreciated.


#6

@ghostlovescore had provide a great explanation, but I would like to chip in a bit.

The if/else syntax can be seen like this:

if (condition) {
    // if condition is true
    // do this code
} else {
    // do this code instead
}

You can have white space in between as long as your syntax is correct, therefore, the code can be written this way too.

if (condition) 
{
    // if condition is true
    // do this code
}
else 
{
    // do this code instead
}

Even though I prefer the first one (I think it's more neat), both are considered correct. Important thing is you must follow the correct order of syntax. Now compare the order with your code.

if (condition) 
{
    // if condition is true
    // do this code
}
else 
{
    // do this code instead
}

if (hungerLevel>7) {
console.log("Time to eat!");
}
else { (hungerLevel<7 )
console.log("Lets eat later");
}

You added another syntax --> ( ) inside else { }, that is wrong and doesn't follow the simple if/else syntax rule. Additional ( ) is not needed here, in this case, you must fully understand if/else function. Your code condition is if (hungerLevel>7), hence, else would mean anything that does not fall into category of >7. You don't need to specify (hungerLevel <7). Unless you want to have further conditions, see my explanation below.


In coming lessons, you will learn there's more complicated situation, thus, you will arrive to what @ghostlovescore proposed.

if moonPhase is full,
then Howwwlll
else if moonPhase isn't full,
then I swear I'm not a werewolf
else
then nothing happens, I'm just a regular human being

That will follow the syntax below ( You will learn it in future ):

if (first condition) {
    // if the first condition is true
    // do this code
} else if  (second condition) {
    // do this code if second condition is true
} else {
    // do this code for the rest
}

Notice where the brackets are ( ), it is outside of curly brackets { }.

However,

that doesn't mean ( ) inside { } is wrong, because there's another syntax where ( ) is inside { }:

if (condition) {
    // if the first condition is true
    // do this code
} else {
    if (condition) {
        // do this condition under 'else'
        // do this code 
    } else {
        // do this code for the rest
    }
}

And that is called nesting, you can really see there's one if/else statement nested inside another if/else statement (again, you will learn it when you progress further).

Most importantly, take the time and fully understand the basic syntax to build solid foundation, so you can use it to build more complex stuff later on.

Sorry to bombard you with new info that might be way ahead of your progress @kingsrook . Just to give you a glimpse of what's waiting for you, have fun! :smiley:


More on Control Flow in JS - 11 - And
Functions & if/ else
#7

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