Sleep Debt Calculator

Working through this project, the code doesnt seem to be work as it says it should…


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

This as i understand is supposed to sum up the total hours assigned for those days.

Rather than get 53 as my answer, it seems to give me all the hours … 58678127 which is obviously giving incorrect results.

What am i missing?

Full code:

const getSleepHours = (day) => {
  switch (day) {
    case "monday":
      return "5";
    case "tuesday":
      return "8";
    case "wednesday":
      return "6";
    case "thursday":
      return "7";
    case "friday":
      return "8";
    case "saturday":
      return "12";
    case "sunday":
      return "7";
  }
};

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

const getIdealSleepHours = () => {
  let idealHours = 8;
  return idealHours * 7;
};

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  if (actualSleepHours === idealSleepHours) {
    console.log('Perfect amount of sleep');
  } else if (actualSleepHours > idealSleepHours) {
    console.log('You got too much sleep! You over slept by ' + (actualSleepHours - idealSleepHours) + ' hours!');
  } else {
    console.log('You should get some rest! You are ' + (idealSleepHours - actualSleepHours) + ' hours under your ideal target!')
  }
};

calculateSleepDebt();

I see three things.

  1. You are missing a closing bracket after you declare getActualSleepHours.
  2. The same function does not return anything.
  3. Your getSleepHours function is returning strings, when they should be numbers.

I think it will work when you correct these issues.

2 Likes

STRINGS OF COURSE!

Thank you for that i was losing my mind! all part of learning i suppose.

Thanks again.

1 Like