Functions and if/else


#1

var sleepCheck = function (numHours){;

    if (numHours >= 8){;
        return "You're getting plenty of sleep! Maybe even too much!"};
        
    else {;
        return "Get some more shut eye"};
        
};

sleepCheck(10);
sleepCheck(5);
sleepCheck(8);

//Please check for errors


#2

When are semicolons used in JavaScript? It seems you have one at the end of every line of code. This is going to create syntax errors that prevent code execution.


#3

Thank you for the help!


#4

Remember to use curly braces/brackets { } when you want to encapsulate a code block. With a function, your format will go something like:

var functionName = function(parameter) {
code code code ;
}


#5

var sleepCheck = function (numHours){

if (numHours >= 8){
    return "You're getting plenty of sleep! Maybe even too much!"}
    
else {
    return "Get some more shut eye"}

}

sleepCheck(10)
sleepCheck(5)
sleepCheck(8)

//Please check for errors

Not working with sleepCheck(5)


#6

There are some good posts on the forums that give tips on syntax guidelines. Here's one for semicolons that you might find useful:

https://www.codecademy.com/blog/78-your-guide-to-semicolons-in-javascript

I think you'll get a feel for where and when to use them as you progress. Also, you can use sites like https://repl.it or https://jsbin.com to practice code on and to see the whys behind error messages you come across.


#7

Some statements end with a semi-colon, I would look up the syntax for the statement I'm wondering about.

For example:

Is missing a semi-colon, because assignment statements end with semicolon! It should look like:

var functionName = function(parameter) {
    code;
};

A function statement on the other hand, does not end with a semi-colon:

function functionName(parameter) {
    code;
}

#8

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