12. Functions & if/else


#1

Hey guys! I'm having trouble with this exercise, and I don't know what I'm doing wrong. Also, what does "unexpected token" mean? Thanks.


#2

Hi megan1441, i seem to be having the same problem, were you able to solve it?


#3

Token (in this error) means the semicolon on line three. To fix this just change line three to:

var sleepCheck = function(numHours) {

#4


#5

I'm not sure how to fix that. I think when you write in console.log it prints whatever you write after it, and in this case we want it to return either one answer or the other, not both. But I'm stuck as well


#6

i changed it to return and that solved the problem.


#7

megan your if statement doesnt have a bracket surrounding it, why dont you try that and your sleepcheck 8 is missing a seicolon


#8


It took me a real long time to get this one. For some reason you have to put everything after the function in {} and finish it with };. Take a look at my screenshot. I am not sure why this is the case as others did not require this to all be inside {}.


#9

Thank you, it worked!!


#10

hello, I seem to be having the same problem.


#11

I was having the same problem. English says there are 2 spaces between sentences--at least, that's how I learned to type. Codecademy wants one space.

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

#12

Hi devrunner,

I can't see the bottom of the code from the snapshot and seems ok but have a look at the code below and see if you can identify where the error in your own may be:

var sleepCheck = function(numHours) 
{

// Inside the function, write an if statement 
 
if(numHours >= 8)
{
    // where if the number of hours of sleep is greater than or equal to 8, the computer will return "You're getting plenty of sleep! Maybe even too much!";.
    
    return "You're getting plenty of sleep! Maybe even too much!";
}
 
else
{
    //Otherwise (else) if the number of hours of sleep is less than 8, have the computer return "Get some more shut eye!";
    
    return "Get some more shut eye!";
}
};

// Then call the function with different hours of sleep
console.log(sleepCheck(10));
console.log(sleepCheck(5));
console.log(sleepCheck(8));

output:
You're getting plenty of sleep! Maybe even too much!
Get some more shut eye!
You're getting plenty of sleep! Maybe even too much!

hope that helps
also adding snapshot to assist you with comparing code


#13

Hello Brother, please help me with this case , I made all the steps and continuous error. Please check my work and help me correct ...


#14

Hey gsus003!,

Trata de quitar el ; de la linea 12 por alguna razon no me lo acceptaba el codigo a mi tampoco de la manera que tu lo tenias escrito.


#15

Just continuing on with @awalklearn's explanation,

You put "Get some more sleep shut eye!" instead of "Get some more shut eye!". :smiley:


#16

Doh! Did not see that. Well spotted. Yeah maybe just do what @ragezapper suggests instead! Haha. My bad!

@gsus003 hola.

quiz√° intente escribir 03 otra vez? Creo que es Cerra lugar de Cierra?

try retyping- 03. { Cerra } ?


#17

You talk spanish?
You can help me?
¬ŅQuien puede ayudarme?
There is my code:
Este es mi código:

//Escribí tu función a continuación.
//¡No te olvides de llamar a tu función!
var controlDescanso = function (cantHoras){
if (cantHoras >= 8){
return("¬°Est√°s durmiendo suficiente! ¬°Demasiado quiz√°s!");
}else{
return("¬°Cerr√° los ojos un poco m√°s!");
};
};
console.log(controlDescanso(10))
console.log(controlDescanso(5))
console.log(controlDescanso(8))

If you can talk to me in spanish is better
Thank you ! :smile_cat:


#18

codemaker46 quita los puntos y coma de las llaves del else y del programa completo


#19

This worked for me:
[It looks like you need "{" after the function is named and "}" with a semicolon after the if/else statement]

// 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!";
}
};

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