Sleep Debt Calculator

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

Hello, i want to ask about Sleep Debt Calculator. I can’t find what’s wrong with my code. It’s always printed out the results i need and ‘undefined’.

const getSleepHours = day => {
  switch(day) {
    case 'monday':
    return 9
    break;

    case 'tuesday':
    return 9
    break;

    case 'wednesday':
    return 9
    break;

    case 'thursday':
    return 9
    break;

    case 'friday':
    return 9
    break;

    case 'saturday':
    return 9
    break;

    case 'sunday':
    return 9
    break;

    default :
    return 'error'
    break;
  }
}
const getActualSleepHours = () => 
  getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday');

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

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

  if (actualSleepHours === idealSleepHours) {
    console.log("You got the perfect amount of sleep!");
  }
  else if (actualSleepHours > idealSleepHours) {
    console.log("You got " + (idealSleepHours - actualSleepHours) + " hour(s) more sleep than you need! You sleep more than you needed");
  }
  else if (actualSleepHours < idealSleepHours) {
    console.log("You got " + (idealSleepHours - actualSleepHours) + " hour(s) less sleep than you need! You need more sleep!");
  }
  else {
    console.log("Error message!");
  };
}
console.log(calculateSleepDebt());

this is the code i made. can anyone help me? thank you

Hi, welcome to the forum.

When you log a function you are in reality logging what that function returns. In this case you do not return anything in your function so are getting undefined.

Try simply calling your funtion, remove the console log call.

2 Likes

thank you so much for helping and reminding me! appreciate it.

2 Likes

Also, when we use return in the case blocks, we do not include break since it is unreachable after return. It is superfluous code.

3 Likes

thank you so much sir for giving me a good knowledge. i’ll make sure to implement it carefully in the future

2 Likes