Hello,
I am totally new to JavaScript and programming. Therefore I am not really sure how some things function.
For example I have the code from the challenge sleepDebtCalculator in the functions topic:
(i’ll only paste the middle and last part of the code)
const getActualSleepHours = () =>
getSleepHours(‘monday’) + getSleepHours(‘tuesday’) + getSleepHours(‘wednesday’) + getSleepHours(‘thursday’) + getSleepHours(‘friday’) + getSleepHours(‘saturday’) + getSleepHours(‘sunday’);
return getActualSleepHours(); // 1. If I add this line of code here I will not be able to see anything else. I remember learning that RETURN ends a process, and I noticed that lower down I could return variables without an issue. Are there cases where return should be called on a function?
const getIdealSleepHours = () => {
const idealHours = 7;
return idealHours * 7;
};
const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
if (actualSleepHours === idealSleepHours) {
return Awesome!
} else if (actualSleepHours > idealSleepHours) {
return You are oversleeping by: ${actualSleepHours - idealSleepHours} hour(s).
} else {
return You need ${idealSleepHours - actualSleepHours} more hours of sleep.
}
};
// In the Tips and Video solution the way they write the above code is by console.log(’’) everything and using + ‘’ +. I found it easier to just write return and add a single console.log for the results (see below) and I proffered using `` {}. The program works fine, but I am curious if it is wrong or if in another case it would be preferable to console log each line separate inside the function or not user the simple {}.
console.log(getActualSleepHours());
console.log(getIdealSleepHours());
console.log(’----------o-----------’);
console.log(calculateSleepDebt());
calculateSleepDebt();
Thank you!