Writing If/Else Statements Within Functions—Introduction to Functions in JS


#1

Hi, Coders!

I am stuck at the following exercise in "Functions & If/Else." Please tell me what is wrong with the following code. (Error message: Unmatched '{')

https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/2/4?curriculum_id=506324b3a7dffd00020bf661#

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

#2

Your sleepCheck function is missing a closing }.

If you indent the else part of your if/else statement to the same level of indentation as the if, it makes errors like this a lot easier to spot.

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

Hopefully that helps!


#3

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