JavaScript Sleep Calculator


I seem to be thoroughly stuck on this project. When I do a console log for getActualSleepHours, I have no error. When I write up one for IdealSleepHours, I do receive an error. I’m sure it’s something simple, but I can’t seem to discern what it is. Any guidance would be much appreciated.

const getSleepHours = day => { switch(day) { case 'monday': return 8 break; case 'tuesday': return 7 break; case 'wednesday': return 8 break; case 'thursday': return 5 break; case 'friday': return 8 break; case 'saturday': return 7 break; case 'sunday': return 8 break; default: return "Error!" } }; const getActualSleepHours = () => getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wedensday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday'); const getIdealSleepHours = () => { const idealHours = 8; return idealHours * 7; }; const calculateSleepDebt = () => { const actualSleepHours = getActualSleepHours(); const idealSleepHours = getIdealSleepHours(); if(actualSleepHours === idealSleepHours) { console.log("You've got the perfect amount of sleep!"); } else if(actualSleepHours > idealSleepHours) { console.log("You've got " + (idealSleepHours - actualSleepHours) + " more sleep than you needed."); } else if(actualSleepHours < idealSleepHours) { console.log("You should get some rest because you slept " + (idealSleepHours - actualSleepHours) + " hours less than you should have this week."); } else { console.log("Error! Something went wrong, check you're code.") }; }; calculateSleepDebt();

a console.log of actualSleepHours and idealSleepHours right before your if statements will reveal the problem.
hint: theres a typo somewhere in the getActualSleepHours function.

1 Like

Thank you for the help, I think the spelling of Wednesday will forever cause me to slip up.

lol. Glad I could help.

I’m really curious about how you were able to debug that. Like what are the actual steps especially since this is a runtime error without any clue as to where the problem lies. I really suffer with debugging, it is my weakest skill. :disappointed_relieved:

  • first at the bottom calculateSleepDept is called.
  • then I asked myself what values does it have…?
  • I noticed it calls getActualSleepHours() and assigns it to actualSleepHours and also calls getIdealSleepHours() and assigns it to idealSleepHours
  • so I console.log(hey over here ---->: ${actualSleepHours}, ${idealSleepHours}) to get a clear picture of what is in there right before the if statement tries to compare the values.
  • I run the code and the console.log returns some a really weird value for actualSleepHours
    hey over here ---->: 15Error!5878, 56
  • so then I say ok actualSleepHours is coming back really weird from getActualSleepHours
  • then I carefully go through getActualSleepHours function looking for typos, syntax errors, or calls to other functions.
  • I notice it does not have any calls to other functions so I don’t need to look further. In either case I would first fix any errors in getActualSleepHours and then run the code again before going any further back.
  • finally after fixing a typo and running the code again it produces the intended result
    note: in this case I was looking at the code backwards from the last line back up. I didn’t actually go all the way back the beginning after I found the problem.
  • If I had still not found what the problem was due to overlooking something I would then look at the code from line 1 and then down carefully through all the code or until I find the problem.

I hope this is not to confusing of an explanation. Patience is the key. If you have been stuck on something for a while take a break relax and come back to it.

1 Like