Problem with 12.Functions & if / else


#1


Please help, i'm stuck!!!

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


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."


My code: 

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


#2

Go over the instructions again and check my comments in your code


#3

Hi biff75,

Thanks for your advise and input, i still seem to be struggling with this...

please see my amended code

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


#4

The instructions ask you to call sleepCheck 3 times.


#5

Still doesn't work when i call it 3 times. i thought i'd try with sleepCheck(10) first to see if that works but no joy.

does the function look correct to you? i'm getting the following error message: Oops, try again. There was a problem with your syntax.


#6

You always need an opening brace and closing brace for all of your function code.


#7

Sooo i've done the following, i am now i'm getting the required outcome,

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

but i'm still getting the 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."

even though my sleepCheck is returning "You're getting plenty of sleep! Maybe even too much!"


#8

Printing is not returning. console.log("text") prints to the console. return "text" will RETURN the string from the function instead of printing it. Change your console.log()s to returns. Then if you want to print what the function returns, you can say:

console.log(sleepCheck(10));

#9

the hint from the instructions

if (condition1) {
return "some string";
}
else {
return "another string";
}


#10

Thanks. the funny thing is i had them as return initially but changed them along the way. thanks for your help.


#11

Thanks. the funny thing is i had them as return initially but changed them along the way. thanks for your help


#12

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

please what's wrong with this code? i get an error message when i run it


#13

Can someone please tell me what is wrong with my code? It writes "SyntaxError: Unexpected token else"
Thanks a lot!

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


#14

Have a look at my note below. hope this helps

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!";
}
}; // try removing ; on this line
sleepCheck(10);
sleepCheck(5);
sleepCheck(8);


#15

see additional notes below

var sleepCheck = function(numHours); { // also remove ; on this line
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);


#16

Have a look at the Hint on this page. it gives you an example of the if / else sybtax.
it also gives you and example of how to call the option to sleepCheck (10), what you are doing at the moment is printing sleepCheck(10).

hope this helps


#17

Thanks man. oshey gaan!. The thing is i'm even way past this lesson but i saw i had to make some completion to this particular one.


#18

Thanks!! I am still trying to work on it.


#20

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