Totally lost on the Sleep Debt Calculator!


#1

Hi there,

I am totally clueless what went wrong!! This is my first time coding and after a few lessons I am stuck more and more frequently, most recently is the Sleep Debt Calculator. Please help!

const getSleepHours = day => {
  if (day === 'monday' || day === 'tuesday' || day === 'wednesday' || day === 'thursday' || day === 'friday' || day === 'saturday' || day === 'sunday')
    return 8;

}

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

function getIdealSleepHours () {
  var idealHours = 8
  return idealHours * 7
}

function calculateSleepDebt () {
  var actualSleepHours = getActualSleepHours();
  var idealSleepHours = getIdealSleepHours();
  if actualSleepHours === idealSleepHours {
    return console.log('you got ideal sleep');
  if actualSleepHours > idealSleepHours {
    return console.log('you slept too much');
    if actualSleepHours < idealSleepHours {
    return console.log('you need more sleep');
  }
  }
  }
  
}

getActualSleepHours();
getIdealSleepHours ();
calculateSleepDebt();

#2

if conditions require parentheses:

if (condition) {
    console.log("do something");
}

the parentheses are missing in your case.

You nested the if clauses, why? If we make the indent proper we can see this:

function calculateSleepDebt () {
  var actualSleepHours = getActualSleepHours();
  var idealSleepHours = getIdealSleepHours();
  if actualSleepHours === idealSleepHours {
    return console.log('you got ideal sleep');
    if actualSleepHours > idealSleepHours {
      return console.log('you slept too much');
      if actualSleepHours < idealSleepHours {
        return console.log('you need more sleep');
      }
    }
  }
}

why would you nest like this? Does this fit the design of your program?


#3

Thanks. Let me try again.

I just enter the lines as I code. I will organise it better with indent.


#4

You should first think how the design/layout/flow of the program should be, before coding. Indent right away


#5

This post was flagged by the community and is temporarily hidden.