Refactoring getActualSleepHours()


I’m not stuck or anything. I was just wondering if there’s a better way to sum all the days in a more automatic way. I tried to do this but couldn’t make it work:

const getActualSleepHours2 = () => {
  const days = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'];
  let sleepSum;
  for (let i = 0; i < days.length; i++) {
    sleepSum += getSleepHours(days[i]);
  return sleepSum;


I’m probably getting something wrong since loops are my biggest weakness. But there must be an even better way than this. I’d be appreciated if someone could help me on this one.


i think i would personally do this:

const monday    = 8;
const tuesday   = 7;
const wednesday = 6;
const thursday  = 8;
const friday    = 5;
const saturday  = 6;
const sunday    = 10;

this is even easier to modify the hours of a specific day, or replace the fixed numbers by something like prompt() to let the user enter the number sleeping hours.

given the days of the week are not going to change, i think its say to put them into an array:

sleepingHoursPerDay = [monday .. sunday];

now you could use a loop, or even .reduce given you use es6.


Thank you. I got rid of the first function altogether and I was able to implement both the loop and the .reduce.