Codecademy Forums

Sleep Debt Calculator: adding results getSleepHours() together

I am on the " Sleep Debt Calculator" task at the moment: https://www.codecademy.com/courses/introduction-to-javascript/projects/sleep-debt-calculator

Here is my code so far:

const getSleepHours = (day) =>{
if (day === 'monday'){
  return 8;
} else if (day === 'tuesday'){
  return 3;
} else if (day === 'wednesday'){
  return 8;
} else if (day === 'thursday'){
  return 4;
} else if (day === 'friday'){
  return 8;
} else if (day === 'saturday'){
  return 6;
} else if (day === 'sunday'){
  return 6;
} else {
  return 'Not a day of the week';
}

  
};

I am not sure how to do the step 5. It says: " Inside the getActualSleepHours() function, call the getSleepHours() function for each day of the week. Add the results together and return the sum using an implicit return ."

As I understand, the function getSleepHours() is to be on one line because we can use implicit return when the function consist of one line.

But how do we add the results of the function getSleepHours() together if they are strings such as ‘monday’, ‘tuesday’ , etc?

Here is one of my attempts to perform the step: const getActualSleepHours= () => getSleepHours() + 7; It returns NaN.

Did I perform the first steps of the task incorrectly?

getSleepHours has a parameter (day), you do not provide any argument when calling getSleepHours (which should just throw an error), so day will be undefined, resulting in 'not a day of the week', trying to add number and string together results in NaN (which again, i think should just throw an error of incompatible types)

2 Likes

Nothing changes if I call the function: getSleepHours(‘monday’); . The result is “Not a day of the week”

Impossible to say what causes the issue without seeing the full code

1 Like

@stetim94 is absolutely correct. You need a ‘day’ argument when you call the function. You want to add the results of calling the function once for each day of the week. If you only wanted the hours for monday and tuesday you would do this:

const getActualSleepHours = () => getSleepHours('monday') + getSleepHours('tuesday');

If you want the hours for the whole week, you just have to add the calls to the getSleepHours() function for the additional days.

5 Likes

I just finished this. it wants you to create another variable, something like this.

const getActualSleepHours = () =>
getSleepHours(‘Monday’) +
getSleepHours(‘Tuesday’) +
getSleepHours(‘Wednesday’) +
getSleepHours(‘Thursday’) +
getSleepHours(‘Friday’) +
getSleepHours(‘Saturday’) +
getSleepHours(‘Sunday’);

4 Likes

I am not sure why these lines appear to be on one line: https://prnt.sc/p4laye .

Well because it is one line of code. This would be similar to
1 + 1 + 2 + 4 + 5

2 Likes

You can also hit enter where you can read the lines the way you feel is more easier. That is how in my previous reply, it looks lined up.

1 Like