Sleep debt calculator

Javascript Functions Sleep Debt Calculator project (https://www.codecademy.com/courses/introduction-to-javascript/projects/sleep-debt-calculator)

In step 5 why am I getting all the sleep hours in list format instead of as a total of sleep hours? Even the person in the project walkthrough gets a total and my code is exactly the same as theirs.

const getSleepHours = day => {
  switch(day) {
    case 'monday':
    console.log(8);
    break;
    case 'tuesday':
    console.log(7);
    break;
    case 'wednesday':
    console.log(5);
    break;
    case 'thursday':
    console.log(9);
    break;
    case 'friday':
    console.log(6);
    break;
    case 'saturday':
    console.log(4);
    break;
    case 'sunday':
    console.log(10);
    break;
    default:
    console.log(day);
    return 'Error!';
  }
};

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

getActualSleepHours();

/*
Prints: 
8
7
5
9
6
4
10
*/

How many times are you calling console.log, and how many outputs are you expecting? Do those counts match?

If you expect one thing to be printed then you should probably also only call console.log once, and only when you have the final result, nowhere else.

1 Like

Ah thank you! That makes so much sense, I tried to solve it myself using the conditionals cheatsheet that has a switch statement example that goes:

const food = 'salad';

switch (food) {
  case 'oyster':
    console.log('The taste of the sea 🦪');
    break;
  case 'pizza':
    console.log('A delicious pie');
    break;
  default:
    console.log('Enjoy your meal');
}

// Prints: Enjoy your meal

Thats why I’d included more than one console.log in my switch statement.
I’ve replaced those with return statements and logged getActualSleepHours() to the console at the end and now my code works perfectly.

const getSleepHours = day => {
  switch(day) {
    case 'monday':
    return 8;
    break;
    case 'tuesday':
    return 7;
    break;
    case 'wednesday':
    return 5;
    break;
    case 'thursday':
    return 9;
    break;
    case 'friday':
    return 6;
    break;
    case 'saturday':
    return 4;
    break;
    case 'sunday':
    return 10;
    break;
    default:
    return 'Error!';
  }
};

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

console.log(getActualSleepHours());

// Prints: 49

put ``` before and after your code on its own line let the forum know it isn’t english text.

what do break and return do? can you break after returning?

a switch is in general not all that useful, it’s more something that tutorials for no good reason feel like they need to cover excessively.
what you have there is key-value pairs, there’s something better suited for that:

const hoursSlept = {
  'monday': 15,
  ...
}