Functions and if else 12 Why doesn't my code work?


#1


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


SyntaxError: Unexpected token if


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)


#2

i still dont understand pls help


#3

My teacher says i have it right but he cant show me cause he changed his to something different


#4

where are the curly brackets ({}) to mark the start and end of your function?

return doesn't require parentheses, but if you use them, close your string before the ) which closes the return

If you need more help, post an updated version of your code


#5

this is almost fully updated since i just changed it


#6

so like 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!)";
}
sleepCheck(10) {
}


#7

look at my function:

var example = function(){
   console.log("function content")
}

see how i use {} to mark start and end of function content? You should do the same (think carefully about what should be inside your function)

my other suggestion:

you did this for 50%:

return ("You're getting plenty of sleep! Maybe even too much!");

now the other 50%:

return("Get some more shut eye!)"; // still wrong

#8

ok ill try to fix it when i can


#9

Also, you're forgetting your semicolons.


#10

if you need more help, post an updated version of your code

@shrike2, which semi-colons?


#11

After his sleepCheck call.


#12

Js will handle it for you, you can give it as recommendation to add the semi-colon, but its not a massive problem.


#13

I am stuck on this same problem but I don't understand why do we have to say var sleepCheck?
This is the code I used.
function 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)


#14

the string returned should be an exact match, copy paste it from the instructions


#15

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