Else-If Syntax Question/Sleep Debt Calculator

In the following code:

const getSleepHours = (day) => {
  switch(day){
    case 'Sunday':
      return 5;
    case 'Monday':
      return 9;
    case 'Tuesday':
      return 7;
    case 'Wednesday':
      return 6;
    case 'Thursday':
      return 7;
    case 'Friday':
      return 11;
    case 'Saturday':
      return 12;
  } 
}

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 = () => {
  let actualSleepHours = getActualSleepHours();
  let idealSleepHours = getIdealSleepHours();

  if (actualSleepHours === idealSleepHours) {
    console.log(`Your hours of sleep: ${actualSleepHours} hours. The ideal hours of sleep: ${idealSleepHours} hours. You are getting the right amount of sleep!`);
  }

  if (actualSleepHours < idealSleepHours) {
    console.log(`Your hours of sleep: ${actualSleepHours} hours. The ideal hours of sleep: ${idealSleepHours} hours. You are not getting enough sleep!`);
  }

  if (actualSleepHours > idealSleepHours) {
    console.log(`Your hours of sleep: ${actualSleepHours} hours. The ideal hours of sleep: ${idealSleepHours} hours. You are getting more than anough sleep!`);
  }
}

calculateSleepDebt();

Why is not formatted as such:

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

  if (actualSleepHours === idealSleepHours) {
    console.log(`Your hours of sleep: ${actualSleepHours} hours. The ideal hours of sleep: ${idealSleepHours} hours. You are getting the right amount of sleep!`);
  } else if (actualSleepHours < idealSleepHours) {
    console.log(`Your hours of sleep: ${actualSleepHours} hours. The ideal hours of sleep: ${idealSleepHours} hours. You are not getting enough sleep!`);
  } else if (actualSleepHours > idealSleepHours) {
    console.log(`Your hours of sleep: ${actualSleepHours} hours. The ideal hours of sleep: ${idealSleepHours} hours. You are getting more than anough sleep!`);
  }
}

I’m new to JS so any insight is appreciated. Thanks!

Eh, preference over anything. Both should work.

1 Like

Aside

Being as the switch is based on a string case and returns a value, we could write that as an object, with the strings as the keys.

const getSleepHours = day => {
  const days = {
    'Sunday': 5,
    'Monday': 9,
    'Tuesday': 7,
    'Wednesday': 6,
    'Thursday': 7,
    'Friday': 11,
    'Saturday': 12
  }
  if (days.hasOwnProperty(day)) {
    return days[day]
  }
  return 0
}

If the lesson expects the switch, then by all means. If we have a choice of logic, I’d be on the side of the lookup table for simplicity.

1 Like

@h1lo

Thanks, I sort of figured it was about preference but glad to know.

@mtf

Thanks for the tip. I’m not really that far yet but I’ll definitely keep it in mind, it makes sense in application though.

1 Like

Wasn’t sure if objects and their methods have been covered yet. My bad. Still, keep this one in your tool kit. Switches have a use, but the above is proven as not one of them since it can be so easily replaced.

Think of a switch as a function that has different execution code for each input. The function doesn’t act upon the input, but is directed by the input as to which code to run. It is one tool in the control flow tool box, and not that universal, as a whole. Choose the use cases wisely.


Silliness

const calc = (fn, a, b) => {
  switch (fn) {
    case 'add': return a + b
    case 'sub': return a - b
    case 'mul': return a * b
    case 'div': return a / b
  }
  return false
}
console.log(
  calc('add', 35, 7),
  calc('sub', 49, 7),
  calc('mul', 6, 7),
  calc('div', 294, 7)
)
42 42 42 42

A pittance for an example though it demonstrates guiding traffic to the correct function to execute for each case. There is a use, as we said earlier.

1 Like

The function doesn’t act upon the input, but is directed by the input as to which code to run.

Thanks, when you put it this way it sort of makes more sense as to why you would write an object versus a switch for the Sleep Debt example. I’ll definitely keep it in mind moving forward.

1 Like