12. Functions & If/else Help


#1

// 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!";
}
console.log(numHours(10));
console.log(numHours(5));
console.log(numHours(8));


#2

We don't read minds!
Got a question?


#3

yeah why do i keep on getting SyntaxError: Unexpected token


#4

Missing { ..postmustbe20chars


#6

I tried that...


#7

Well you'll have to agree that it should be there, so don't remove it again!

That's not the only such mismatch you've got. For each opening bracket of some kind there should be a closing one.
Oh nvm, that wasn't it.

I'll keep looking..

You're also using numHours before having defined it? That's probably not a SyntaxError but it definitely doesn't look right.

You're returning from outside a function. That does not make sense.

Your first line might not make sense to JS either, I wouldn't know though, don't know any JS, perhaps that does something fancy.


#8

The function call should be something 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!");
}

console.log(sleepCheck(10));
console.log(sleepCheck(5));
console.log(sleepCheck(8));

However I'm also getting a syntax error...


#9

You're not meant to console.log the parameters at the end, and you're also missing a } at the end of the function (you close the else statement, but not the function). It works this way:

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

#10

Yes, you're right! Thanks!

I'm finding that one has to do exactly what is expected, otherwise one will get an error, even if there's none.

But on other occasions you can get away with whatever you do :smile: