Sleep Debt Calculator - undefined

Hello,
Both of my codes showing undefinded. Please help me take a look wts going wrong. Thx!
console.log(getActualSleepHours());

console.log(getIdealSleepHours());

const getSleepHours =day=>{
if (day===('monday'))
{return 8; }
else if (day ==='tuesday')
{return 5;}
else if (day ==='Wed')
{return 0;}
else if (day ==='Thur')
{return 12;}

else if (day ==='Fri')
{return 12;}
else if (day ==='Sat')
{return 12;}
else{"error"}
};
 
const getActualSleepHours = () =>{
getSleepHours('monday')+
getSleepHours('tuesday')+
getSleepHours('Wed')+
getSleepHours('Thur')+
getSleepHours('Fri')+
getSleepHours('Sat');
}

const getIdealSleepHours  = () =>{
const idealHours =10;
return 
idealHours*6;
};
console.log(getActualSleepHours());

console.log(getIdealSleepHours());```

In getIdealSleepHours,

// You wrote:
return
idealHours*6;

// It should be:
return idealHours*6;

The value being returned isn’t on the same line as the return keyword, so idealHours*6 is not considered a part of the return statement. Therefore, getIdealSleepHours returns undefined.

In getActualSleepHours,

// You wrote:
const getActualSleepHours = () =>{
getSleepHours('monday')+
getSleepHours('tuesday')+
...
}

// It should be:
const getActualSleepHours = () => {
return getSleepHours('monday')+
getSleepHours('tuesday')+
...
}

Since you are using curly braces for the body of getActualSleepHours, so you need to use the return keyword for an explicit return. If you want to omit the return keyword and do an implicit return, you need to remove the curly braces,

// This will also work:
const getActualSleepHours = () => getSleepHours('monday') +
getSleepHours('tuesday') +
...
2 Likes

Thank You so much. However, I still have another question.
I already set actualSleepHours and idealSleepHours on 35 and 36
However, it is still showing not defined.
May I know why?

Thank You so much. However, I still have another question.
I already set actualSleepHours and idealSleepHours on 35 and 36
However, it is still showing not defined.
May I know why?

That happens because you are initially declaring the actualSleepHours variable inside a function block instead of making it global.

This means that the actual variable can only be used inside the function block hence the console throws you the error of not knowing what actualSleepHours is on Line 42.

I hope I helped you.

2 Likes

Hello,

I have a quick question.

May I know what is the difference of with and without parentheses ?

const calculate (day) => { };
const calculate day => { };

What do you mean exactly? Because both of the sentences are wrong.

Arrow functions should be declared like that.

const calculate = (day) => {//yourcode}

or

const calculate = day => {//yourcode}

To put it simply, when you have multiple arguments you use parentheses, whilst when you have only one argument you can use no parentheses.

Refer to the arrow function lesson in Codecademy.

2 Likes

Got it!!!
Thanks for answering.

1 Like