Help please! Page 12 - function & if / else


#1

Hello JavaScript fans

Firstly, this course is really fun and not too difficult to learn.

However the code I am writing in for my function (shown below) is incorrect.

Please can somebody help me to solve my problem?

Source code:

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

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

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

Output:

"Get some more shut eye!"

Error message:

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.

I would be very grateful for the help I receive.

Kind regards

HTDynamicDude


#2

The syntax seems to be ok probably a typo in the output message. First guess would be an additional space between ! and Maybe. Also you may wrap your function calls in console.log otherwise you only get the response echo for the last value in your code.


#3

Thank you for your help, haxor789. :slightly_smiling:
Actually, your problem solving has allowed me to complete the 1st JS functions course.

My entire source code now works.

You are right, I had put an extra space between the "!" and "Maybe", inside the if code block.

I have also corrected the numHours parameter to meet the condition more than or equal to 8, instead of 10.

My console.logs look like this:

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

My entire source code looks like this:

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

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