JavaScript # 12 Functions If Else Function Not Working


#1

I encounter the error below. What might be wrong with my syntax?

I have tried every other way to write the line of code, however still encounter the same error.
The output of the return statement works. That is why I am a bit confused.

Here is my line of code:

// Write your function below.
// Don't forget to call your function!

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

sleepCheck(10);

The weird thing here is that there is no extra curly brace, no other missing characters, but still the error on my the console displays the error below.

Error below:

Oops, try again. It looks like sleepCheck() isn't returning "You're getting plenty of sleep! Maybe even too much!" when numHours is 10. Check your if / else syntax and whether you're using the correct comparison operator.

The issue is still not resolve, I do not know what part of the code is missing. even the operator I used is correct.


#3

Please post Link to the lesson and your code.
NO Screenshot of it.


#4

This is my code and I keep getting an "unexpected end of input error"

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)


#5

@byteninja78735

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);

your codes look ok only few changes you need

1...there is closing curly bracket at the end of code

2...while calling a function you forgot to put semicolon
***** it will work****

After running this code you will get "You're getting plenty of sleep! Maybe even too much!" this line of code because javascript run top to bottom manner so it will show result of last function call.
If you want to print result of all 3 function call then use console.log and before that you need to store value returned from sleepCheck to some other variable which will be passed to console.log as argument.
like
var sleepResult=sleepCheck(8);
console.log(sleepResult);

-----------------------------Happy Coding ---Keep Coding----------------------------------------


#6

Now I found it, I think the console generates error even for the slightest spaces..

The issue was this line of code:
return "You're getting plenty of sleep! Maybe even too much!";

Instead of having this I had some spaces between "too" and "much".]

The line of code looked like this:
return "You're getting plenty of sleep! Maybe even too
much!";

This means that the console or the editor is likely space-sensitive? if that word even exists.
So what I could also suggest is copy and paste the exact word in the editor.


#7

Try ; after your last } Also, not sure if they are sticklers for the way theyve been teaching you to call functions until this point, but you could try var sleepCheck=function(numHours) {

This worked for me, I had to count the spaces between my words carefully.

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);