Functions & if / else (#12) HELP


#1

I am getting an error and not sure why? I have looked through the forum and still cannot figure it out. Any help is greatly appreciated. Here is my code:

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

#2

Let's highlight the first { and its corresponding closing brace:

var sleepCheck=function(numHours){ // <<< Starts here
    if(numHours>=8)
    return "You're geting plenty of sleep! Maybe even too much!";
} // <<< ends here
else{
    
    return "Get some more shut eye!";
}

This means that your function ends before the else. If you move the else inside the function, your error should be resolved


#3

Great! That worked @nicoekkart thank you so much. And while I was waiting for some help I went over the code again carefully and caught that "getting" was misspelled ('getting'). Thanks again.


#4

Good catch! Great too hear that you weren't just laying around doing nothing :smile:


#5

var sleepCheck = function(numHours)
if(numHours >= 8){
return "You're getting plenty of sleep!";
}
else {
return " Get some more shut eye!";
}

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

I tried else inside the function curly brackets but didn't work.
I tried console.log check as well. nothing seems to be working. :frowning:


#6

I'm having this same issue and I cant see any mistake in my code (getting this error: SyntaxError: Unexpected token ILLEGAL):

// Write your function below.
// Don't forget to call your function!
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));


#7

First of all, you should surround your function with braces like you did at first.
Then you should check if you're returning the correct strings "You're getting plenty of sleep! Maybe even too much!" and "Get some more shut eye!".

I would suggest just copying the strings.


#8

You're missing a " after your first string.


#9

Hi there, I don't understand why I am getting the right message at the console but not from the Exercises System. Can anyone help?


#10

// Write your function below.
// Don't forget to call your function!
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);

Still Giving me a syntax error.


#11

@shilpabadve

Where are your curly bracket starting your function?

You didn't close the curly brackets for the function.

Hint

Remember a function syntax looks like this:

var functionName = function(optional arguments) {
// all the code runs in the bracket
};

#12

got it. thank you.
Curley braces start after optional argument and end at the last sleep check.


#13

Same here. I cannot figure out for the life of me what is wrong. The console says "Syntax Error: Unexpected Token else."

// Write your function below.
// Don't forget to call your function!
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);

This is my code, cannot figure out what the mistake is???


#14

this way:
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));