#12 functions & 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!");
};

console.log(sleepCheck(10);
console.log(sleepCheck(5);
console.log(sleepCheck(8);

whats wrong with this? someone please tell me i've tried editing it after looking at others but still nothing and its frustrating me


#2

Syntax error. Add one more ) to each line.


#3

console.log(sleepCheck(10));

like that? if so i did just that but it says
SyntaxError: Unexpected end of input


#4

This means the function is not closed. Add one more }; to the end of the function body. Actually, the line above that would not have a ; since if statements do not need them.

    }
};

#5

i appreciate the help but i just don't get what you're trying to tell me :// its just me sobs


#6

Your function does not have a closing brace.

var sleepCheck = function (numHours) {
    if () {
        //
    } else {
        //
    }
};

#7

still nothing i'll just move on then trying to understand this is hurting my head


#8

Try this, then:

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!";
    }
};

#9

i am upset, it worked but upset that i wasn't able to do it myself... those stupid curly braces :frowning:


#10

The way around this is to practice building empty shells. what I call blocking, and then populate the shells from the inside out (rather than doing it top down).

This is an example taken from above but left naked:

var _name_ = function () {
    if (_condition_) {
        //
    } else {
        //
    }
};

It gives us all the structure we need to complete the build, and the core syntax is in place. Bring in a variable name and a parameter list (zero or more variables) and we have the shell of a function expression. Now we fill in the if statement and we're away.

More complex builds, such as Rock, Paper, Scissors, require more extensive blocking. This can and should be done first, and the code bits added later, when the structure is in place.

We can invoke a block even with no code in it yet.

var function foo () {};
console.log(foo());     // undefined

No syntax errors thrown? Now we can expand it.

var condition;          // temporary variable
var function foo () {
    if (condition) {

    } else {

    }
};
console.log(foo());     // undefined

Still no syntax errors? Contine the build and test again. Taking a step by step approach greatly improves productivity and and reduces incidental errors that may otherwise frequently occur. It's all in the practice.


#13

I have been trying for the longest while to figure out what is wrong with this code:

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!";
    }
};
console.log(sleepCheck(10));
console.log(sleepCheck(5));
console.log(sleepCheck(8));

Could you help me out please?


#14

Please post a link to this exercise. Thank you.


#15

https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/2/4


#16

I just passed with your exact code. Perhaps a page refresh will help. Sorry I cannot offer more assistance.