Sleep Debt Calculator & Concise Body Arrow Function


#1

Going through the Sleep Debt calculator project in javascript here:

https://www.codecademy.com/courses/introduction-to-javascript/projects/sleep-debt-calculator

and in one of the parts, they use a “Concise Body Arrow Function”

from the previous lessons, codecademy mentions that this specific syntax can only be a single line, therefore does not need curly braces or a return statement, yet in the guide, they use this syntax with multiple lines like this:

const getActualSleepHours = () =>
getSleepHours(‘Sunday’) +
getSleepHours(‘Monday’) +
getSleepHours(‘Tuesday’) +
getSleepHours(‘Wednesday’) +
getSleepHours(‘Thursday’) +
getSleepHours(‘Friday’) +
getSleepHours(‘Saturday’)

can someone explain to me how this is allowed, and how it works?


#2
const getActualSleepHours = () =>
getSleepHours(‘Sunday’) +
getSleepHours(‘Monday’) +
getSleepHours(‘Tuesday’) +
getSleepHours(‘Wednesday’) +
getSleepHours(‘Thursday’) +
getSleepHours(‘Friday’) +
getSleepHours(‘Saturday’);
const getActualSleepHours = () => getSleepHours(‘Sunday’) + getSleepHours(‘Monday’) + getSleepHours(‘Tuesday’) + getSleepHours(‘Wednesday’) + getSleepHours(‘Thursday’) + getSleepHours(‘Friday’) + getSleepHours(‘Saturday’);

These two are the same. They are only 1 line of code. Because they have +'s in between, the results of all of the times the getSleepHours is executed gets added together and the result will be a single (1) number value.

This would’ve been a different case if it were written as follows:

getSleepHours(‘Sunday’);
getSleepHours(‘Monday’);
getSleepHours(‘Tuesday’);
getSleepHours(‘Wednesday’);
getSleepHours(‘Thursday’);
getSleepHours(‘Friday’);
getSleepHours(‘Saturday’);

It works the same with strings:

let myName = 'louwding';

let myStringOne = 'My name is ' + myName;

let myStringTwo = 'This is my name: ' +
  myName;

console.log(myStringOne);
// Returns: My name is louwding

console.log(myStringTwo);
// Returns: This is my name: louwding

This is because you add (or concatenate) the answers (results) together.


#3

that makes sense, thank you!