Sleep Debt Calculator

I need a steer in the right direction please.

Doing this exercise: Sleep Debt Calculator.

My problem is I’m getting:

ReferenceError idealSleepHours is not defined

on this code when calling calculateSleepDebt();

const getSleepHours = day => {
  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':
      return 2;
      break;
    default:
      return 'invalid entry, please enter a day of the week';
  }  
};

const getActualSleepHours = () =>
getSleepHours('monday') +           getSleepHours('tuesday') +
getSleepHours('wednesday') +
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 got EXACTLY the right amount of sleep!');
  } else if (actualSleepHours > idealSleepHours) {
    console.log('You got MORE sleep than you needed!');
  } else {
      console.log('GET SOME SLEEP');
  }
};

I’ve been staring at it for ages now, and my own research isn’t helping.

Can anyone push me in the right direction so I can work it out please.

Spelling and capitalization are crucial when referring to variables. Your error says that idealSleepHours is not defined. Try looking at where it is supposed to be defined. Remember, close doesn’t count.
helloWorld is not the same as helloworld.

also, does string->value remind you of anything?

{ monday: 8
, tuesday: 16
, ...
}
getSleepHours('monday') +           getSleepHours('tuesday') +
getSleepHours('wednesday') +
getSleepHours('thursday') +
getSleepHours('friday') +
getSleepHours('saturday') +
getSleepHours('sunday');

this has a whole lot of repetition, how about:

[ 'monday'
, 'tuesday'
, 'wednesday'
, 'thursday'
, 'friday'
, 'saturday'
, 'sunday'
].map(getSleepHours)

which would get you an array of numbers that you could then sum up:

[ 'monday'
, 'tuesday'
, 'wednesday'
, 'thursday'
, 'friday'
, 'saturday'
, 'sunday'
].map(getSleepHours)
 .reduce((a, b) => a + b)
1 Like

I think here is the problem. You made your function idealSleepHours(Uppercase H) and here you wrote idealSleephours. Since JS is case sensitive, you have a both function and a new variable. In my exercise, I wrote both with uppercase H and it works fine. Try it out.

OMG. I’m so sorry. Such an obvious one. I was so tired last night and I forced myself to start the project just to get another one done before bed. Checking the case ws one of the first things I had on my list to problem solve with.

Thanks and sorry for wasting your time…

Thanks for the reply!
Great tip on not needing break, I was using the functions cheat sheet to get the syntax correct - it has the break line after each case. But as you’ve pointed out I’m using a return so it ends there anyway.

Could you ask me again about your string->value point please. I’m not quite following what you’re driving at.

Your final bit about streamlining my code looks code as well - at this point in the web development course I’ve not come across arrays. I’ve just had a look ahead and it’s coming up further down the line. I’ll save this project and come back to it when I’ve completed that section and apply my knowledge.

Thanks again.

Objects associate strings with values, if you have a string, then you can look up that string in the object to obtain the corresponding value

In this case you have a weekday which you would use an object to associate with a number.

Your switch is code that says what to do, but you could replace this with data, and look up the part of the data that you care about (code and data are roughly equivalent things, but data is more flexible when it comes to making operations on them… structured data is also less error prone than code (can’t have bugs in data), it’s all kinds of nicer if you have a choice)

If you consider what your switch looks like in memory, then, it’s data + code, but the code is repeated over and over even though it’s only really needed once, so, as an alternative you could store only the data, and then have a separate piece of code for navigating that data

1 Like

That makes sense, thanks again.

is another example of code vs data

getSleepHours is a function… it’s code, but, here it is treated as a value, passed to map so that map can use it

using functions as values lets us do things like delay their use or combine several of them into new ones

you’ve either already covered loops or you’re just about to… and that’s a whole language feature to make something happen, but, map also represents iteration and it didn’t require any extra help from the language

reduce also does iteration… it holds on to a single value, and combines it with each element, so, for example to compute a sum, you’d start with 0, and combine zero using ADD (a function) with each of the elements that you’re summing

Yes loops is in the same component as arrays.

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