12.Functions & if / else cant find the problem


#1

cant find what the problem is
the error code its giving me is
"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."

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

#3

I am reworking this problem now, so I'll list my steps as follows:

  1. Start on line four and use the key word function to tell the computer that what's next is a function.
    This is the simpler way for me. Anyways, I would write it in a way such as
    function sleepCheck(numHours) {
    You did this except for you started with var sleepCheck, which definitely works, but I'm just showing you my process and I'm explaining why mine's different.
  2. Create your if statement.
    What I do is make sure it's indented, (it should automatically indent for you), and write if and then put the condition inside of parenthesis like so:
    if (numHours > 7) {
    Don't forget the open bracket! You can write it using =< 8 or > 7.
  3. Finish your if statement.
    Tell it to return by using the keyword return and then using parenthesis. My example says:
    return("You're getting plenty of sleep! Maybe even too much!");
    My semicolon signals an end. On the next line don't forget to enter a close curly bracket. Additionally, it needs to line up with the beginning of the if statement which should be indented once.
  4. Write your else statement.
    Start your else statement by lining the beginning of it up with the beginning of the if statement. This is all inside of the function so we need to make sure our syntax shows that. My else statement looks like this:
    else {
    (indent) return("Get some more shut eye!");
  5. Lastly, call your function using the numbers 10, 5, and 8 which you did perfectly.

SOME NOTES

  • Make sure that your close curly bracket, }, is in line with if or else in the case that it is closing either of those!

  • Use parenthesis with the return keyword like you use parenthesis when you use console.log (This is optional but it does work so I did it. I tried it both ways and it doesn't make a difference.)

Please let me know if this helps!


#4

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