Sleep debt calculator - won't add hours

Hello all,

I am new to coding and new to the forum, so please forgive me if I have posted anything incorrectly.

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

I am struggling with Step 5 of this project (specifically this bit " Add the results together and return the sum using an implicit return .")
Here is what I have -

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 '7';
  } else if (day === 'friday') {
    return '8';
  } else if (day === 'saturday') {
    return '6';
  } else if (day === 'sunday') {
    return '7'; 
  } else {
    return 'invalid'
  }
};

const getActualSleepHours = () =>
getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday');

// These all return the correct hours
console.log(getSleepHours('monday'));
console.log(getSleepHours('tuesday'));
console.log(getSleepHours('wednesday'));
console.log(getSleepHours('thursday'));
console.log(getSleepHours('friday'));
console.log(getSleepHours('saturday'));
console.log(getSleepHours('sunday'));

//This doesn't add the hours, just returns a list of them
console.log(getActualSleepHours());

I don’t get any errors messages, just a list of 8767867 instead of 49. What have I done wrong/ how do I correct it?

given getSleepHours returns string, doing + will give string concatenation:

console.log('1' + '1') // 11

if you want sum, you will have to use numbers:

console.log(1 + 1) // 2

I have fiddled with the code and now have this -

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 '7';
  } else if (day === 'friday') {
    return '8';
  } else if (day === 'saturday') {
    return '6';
  } else if (day === 'sunday') {
    return '7'; 
  }
};

const getActualSleepHours = () =>
getSleepHours(8) + getSleepHours(7) + getSleepHours(6) + getSleepHours(7) + getSleepHours(8) + getSleepHours(6) + getSleepHours(7);

// These all return the correct hours
console.log(getSleepHours('monday'));
console.log(getSleepHours('tuesday'));
console.log(getSleepHours('wednesday'));
console.log(getSleepHours('thursday'));
console.log(getSleepHours('friday'));
console.log(getSleepHours('saturday'));
console.log(getSleepHours('sunday'));

//This doesn't add the hours, now returns NaN
console.log(getActualSleepHours());

It is now however logging ‘NaN’.

When I had the last ‘else’ it would just log ‘invalidinvalidinvalid…’

  } else {
    return 'invalid'
  }
};

Or am I completely misinterpreting and need to code as

console.log(getSleepHours 8 + 7 + 6 + 7 + 8 + 6 + 7);

here:


const getActualSleepHours = () =>
getSleepHours(8) + getSleepHours(7) + getSleepHours(6) + getSleepHours(7) + getSleepHours(8) + getSleepHours(6) + getSleepHours(7)

you know have numbers, why?

I have probably misinterpreted what you meant here:

step 5 is saying to use the getSleepHours function for each day, to me that is saying I have to code it. Along with your advice of using numbers to get a result, this is what I have ended up with.

As I can see now after sleeping on it this provides me with the correct result:

console.log(8 + 7 + 6 + 7 + 8 + 6 + 7);

I’m still not sure where I went wrong in the first place, as the hint for step 5 is to use getSleepHours('Monday') + getSleepHours('Tuesday') but this doesn’t work for my code and your advice is saying it will return a string concatenation. Would you be able to help me understand my error(s), as I can’t see what is going wrong?

that is because your function return strings:

  if (day === 'monday') {
    return '8';
  } else if (day === 'tuesday') {
    return '7';
  } else if (day === 'wednesday') {
    return '6';
   } else if (day === 'thursday') {
    return '7';
  } else if (day === 'friday') {
    return '8';
  } else if (day === 'saturday') {
    return '6';
  } else if (day === 'sunday') {
    return '7'; 
  }

I merely demonstrated what happens when you use + on strings (resulting in concatenation)

Oh of course it does! I understand now, I don’t know why I couldn’t see that. Thank you for your help, very appreciated :relaxed:

1 Like

Because I kept my answer deliberately a bit vague. Figuring things out on your own is so important, so I merely try to provide guidance.

1 Like

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