Help on "Functions & if / else"


#1

I am really confused on this lesson.

Introduction to Functions in JS
Lesson Name: Functions & if / else
Here is my code

// Write your function below. 
// Don't forget to call your function!
var sleepCheck = function( numHours ) {
        if ( sleepCheck >= 20 ); {
                return "You're getting plenty of sleep! Maybe even too much!";
        }
        else ( sleepCheck < 10 ); {
                return "Get some more shut eye!";
        }
};

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

Output: "Get some more shut eye!"
Error: "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."

It may just be me not understanding the lesson correctly. If someone could explain this
lesson more easily, that would help a bunch!

I posted something like this before but I went over seas to visit mother, so this topic closed when I got back.

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


#2

Hi @christopherdoescodin,

There is problem with your if/else syntax.

look at syntax below..

if (condition) {
  //yourcode
} else {
  //yourcode
}

We don't use semicolons after putting condition of if statement.
else part goes without any condition.

also Take a look at instruction,
1.The string that youre returning should be exact match as mentioned in exercise!
2. check the conditional expression of if statement


#3

Here is my old post.


I have read what "rcodeman" said, but I had trouble understanding it.

And greentreemee, I am still having problems understanding it.
Updated code:

// Write your function below. 
// Don't forget to call your function!
var sleepCheck = function( numHours ) {
        if ( sleepCheck >= 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 );

#4

Sorry but when I was away overseas to visit my mother, I did not really have time to script or code anything. I forgot some things


#5

Here...

if ( sleepCheck >= 10 )

Your functions parameter is numHours.

also It says this in instruction...

Inside the function, write an if statement where if the number of hours of sleep is greater than or equal to 8, the computer will return "You're getting plenty of sleep! Maybe even too much!";

So we need to make change in numbers of hours too ?


#6

I have fixed it, seems like my brother talks my language :smile:
He is better than me at scripting coding.

Thank you for helping me :smiley:

Could someone close this post?


#7

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