12.Functions & if/else


#1

Hello what is wrong in my code?
In console say:"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);
sleepCheck(5);
sleepCheck(8);


#2

look at my function:

var example = function(){
   // function content
}

see how i use curly brackets ({}) to mark the start and end of my function? They are missing in your case


#3

ok, thanks but where I put the curly brackets?


#4

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

Don't work....


#5

in console say:

"SyntaxError: Unexpected token else"


#6

i left something for you to discover, what should be part of your function content.

here:

if (numHours >= 8)  // missing {

why did you remove the { of if?

once you added that one, don't forget to add a } at the right place to close your function


#7

Don't work...Thanks for help but not work...
Now mycode is:

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

and in console say:

"SyntaxError: Unexpected end of input"


#8

to be more specific, look at your function:

var SleepCheck = function(numHours) {

the { of your function doesn't have a matching } to close the function, place } at the right place


#9

Don't work....Thanks u so much but not work!

Look at my code:

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

and in console say:

SyntaxError: Unexpected end of input


#10

hm... look:

var SleepCheck = function(numHours) {
if (numHours >= 8){
return ("You're getting plenty of sleep! Maybe even too much!");
}

what you highlighted closes the if content, not the function. Besides, you want else to be part of your function, right?


#11

I'm puzzled...What I need to do?


#12

you need to close your function by adding a } at the right place


#13

Thanks but where is the right place???This is my question


#14

but if i tell you, i just provide you with the solution, where do you think you should place it? I am trying to help, not provide you with the solution

from the instructions:

2 . Inside the function, write an if statement
3 . Otherwise (else

so point 2 and 3 of the instructions should be inside the function.


#15

You named the variable SleepCheck, but you call sleepCheck... It`s important that you always have to use the exact same name!


#18

var sleepCheck = function(numHours) {
if(numHours>=8) {
return("You're gettin gplenty of sleep! Maybe even too much!");
}
else {
return("Get some more shut eye!");
}

}

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

not working


#19

the string you return needs to be an exact match, copy it from the instructions. If you need more help, please create your own topic (not a reply to this topic)


#22

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