"Sleep Debt Calculator" project

I’m having trouble with the project “Sleep Debt Calculator”. This is what I have for it so far but it keeps having an error in the code. I don’t know what I am doing.

const getSleepHours => {

if(day === ‘monday’) {

return 8;

} else if (day === ‘tuesday’) {

return 7;

} else if (day === ‘wednesday’) {

return 6;

} else if (day === ‘thursday’) {

return 5;

} else if (day === ‘friday’) {

return 4;

} else if (day === ‘saturday’) {

return 3;

} else if (day === ‘sunday’) {

return 2;

default:

return 'Error!';

}

else {

}

switch (day) {

case 'monday':

return 8

break;

case 'tuesday':

return 7

break;

case 'wednesday':

return 6

break;

case 'thursday'

return 5

break;

case 'friday'

return 4

break;

case 'saturday'

return 3

break;

case 'sunday'

}

};

Hey @blog0369955490

Which step are you stuck at? :grinning_face_with_smiling_eyes: Remember to log your code, to test if everything works properly/as you want it to. Especially in these small projects we get, because we can’t get an error if it’s incorrect, just a syntax error.

I don’t think you need an empty else statement (you can just delete that one, since it does nothing anyway). And it seems that you have both if/else if & switch statements together in 1 function. But both of them are doing the same thing :man_shrugging:

I know that this wasn’t of much help, but try to figure it out yourself. If you’re stuck at a certain step, just go practice the previous lessons and try to connect the dots. Or study from different sources on the same topic. It takes time on the side, but for me it was really helpful.

Let us know if you’re stuck again!

Cheers

note: JavaScript, not Java

It looks like you’re trying to do a switch block and a bunch of if-else stuff at the same time
but you only need one of those things here (use either the switch statement only, or the if-else stuff only)
Note that the default does not belong inside of an else-if
default: should only be in the switch block

Also, the getSleepHours function is missing the parameter (day)

// JavaScript const getSleepHours = day => { if (day === 'monday') { return 8; } else if (day === 'tuesday') { return 7; } else if (day === 'wednesday') { return 6; } else if (day === 'thursday') { return 5; } else if (day === 'friday') { return 4; } else if (day === 'saturday') { return 3; } else if (day === 'sunday') { return 2; } else { return 'Error!'; } } let when = 'monday'; console.log(when); console.log(getSleepHours(when));

https://www.codecademy.com/courses/introduction-to-javascript/projects/sleep-debt-calculator

For practice, you could try to do it using a switch statement instead of a bunch of if-else stuff too.

1 Like

For context, this topic was originally in Get Help >> Java.

1 Like

I have since figured out what was wrong by doing it over again. Thank you all!