A few beginer's questions about Functions and best practices in JavaScript


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 {}.

Thank you!


Not more than you already described i think. Here is the documentation if you want to read up on it.

It depends what you use console.log for. If you use it for debugging you just want to paste it in there since its temporary and has to be removed anyway after you’re done. In this case you don’t want to rebuild your code to not use returns.
If you use console.log to get the output/feedback you’re method would be better. Big change the return statements gonna stay anyway when you change the output system.

In you’re case i would say your method is better.


Thank you! And thank you for the reference link!