Sleep Debt Collector

  • In this exercise we will calculate if you are getting enough sleep each week by using a sleep debt calculator.

  • The program will calculate the ideal and actual sleeping hours of each night and will tell if you are getting enough sleep or not.

Solution Code:

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;
  }
};

// console.log(getSleepHours('sunday'));

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

// return sleepHours;

// console.log(getActualSleepHours());

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

// console.log(getIdealSleepHours());

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();

  const idealSleepHours = getIdealSleepHours();

  if (actualSleepHours === idealSleepHours) {
    console.log(`The user got the perfect amount of sleep.`);
  } else if (actualSleepHours > idealSleepHours) {
    console.log(`The user more sleep than needed.`);
  } else {
    console.log(`The user should get some rest.`);
  }
  if (actualSleepHours < idealSleepHours) {
    console.log(
      `The user got ${
        idealSleepHours - actualSleepHours
      } hours less than the ideal sleeping hours.`
    );
  } else {
    console.log(
      `The user got ${
        actualSleepHours - idealSleepHours
      } hours more than the ideal sleeping hours.`
    );
  }
};

calculateSleepDebt();

Last updated