12. Functions & if / else Help


#1

The error says: Oops, try again. There was a problem with your syntax.


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

How to fix this?


#2

Hello

You need a function, add the word function before (numHours).

var sleepCheck = function (numHours)

 {

 if (numHours >= 8)

{

 return ................

Hope that helps. If it did do not forget to mark this topic as solved. If you get stuck let me know.


#3

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

Mine isnt working as well.

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.


#5

Hello

In the instructions we did not need to create a var sleep, nor return sleep.
I have not done it yet, but I am pretty sure console.log is used in loops, this is a function. We want to return a message if the condition is true, if the condition in if is not met, the else return message is displayed, when we call on the 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);

I hope that helped.


#6

Please remember the following regarding topics -

Thanks


#7

Yep as @carl1n said you should be using return rather than console.log. I would ask carl, if you could refrain from posting the full solution next time, just describing how to fix the problem as you did do should help enough.

The other thing @avrona is in this line

You should not have a ; here as it is not the end of the statement. The ; goes after the closing }. So the basic function should look like:

var basicFunction = function (parameter) {
  // Some code
} ;

#8

Thank you for bringing it to my attention.
I will definitely do that. I was not sure if I described how to fix the problem was enough, so I added the code. But truly thank you, that helps.


#9

what is the problem with this code. can't move to next lesson :frowning:

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

#10

Hello

Spelling is the problem.

The instructions say type the following -

return "You're getting plenty of sleep! Maybe even too much!";.
You typed - return "You are getting plenty of sleep! May be even too much!"

Hope that helps


#11

Thank you. I could have copypasted instead of typing myself.


#12

What's wrong please...?

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


#13

Before the line where you are calling your function, you forgot to close your function with }.


#14

Sorry to say I don't understand.


#15

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


#16

your code should be this:

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!";
}
} //your error was you forgot this bracket
sleepCheck(10);

#17

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