# SleepDebt Calculator/ JavaScript functions

Hi Community,

could anybody help me out to solve the exercise?
This is my code…

``````const getSleepHours = (day) => {
switch (day) {
case 'Monday':
return 6;
break;

case 'Tuesday':
return 5.5;
break;

case 'Wednesday':
return 7;
break;

case 'Thursday':
return 6.5;
break;

case 'Friday':
return 8;
break;

case 'Saturday':
return 5;
break;

case 'Sunday':
return 6;
break;
}
};

const getActualSleepHours = () => {
getSleepHours('Monday') + getSleepHours('Tuesday') + getSleepHours('Wednesday') + getSleepHours('Thursday') + getSleepHours('Friday') + getSleepHours('Saturday') + getSleepHours('Sunday');
return getSleepHours();
};

const getIdealSleepHours = () => {
let idealHours = 6;
return idealHours * 7;
};

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();

if(actualSleepHours === idealSleepHours) {
console.log('You got the right amount of sleep !!');

} else if(actualSleepHours > idealSleepHours) {
console.log('You got ' + (actualSleepHours - idealSleepHours) + 'more sleep than needed.');

} else (actualSleepHours < idealSleepHours)
console.log('You got' + (actualSleepHours - idealSleepHours) + ' hours of sleep. That is too less and you need more sleep, take a rest !!');

};

console.log(calculateSleepDebt());
``````

After I call the calculateDebt function this is my outpout…

You gotNaN hours of sleep. That is too less and you need more sleep, take a rest !!
undefined

Does anyone have a solution to solve the problem ??
Thanks for helping me !!

Greetings Mike

here:

``````const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
console.log("actualSleepHours value: " + actualSleepHours);
``````

i added a console.log, `actualSleepHours` has a value of undefined. Oops, it seems `getActualSleepHours` returned undefined, that can’t be right. What could be wrong?

That doesn’t help me, I didn’t understand what you meant.
What is wrong with my code??

And one more question, how did you put the code into a scrollbox ?? For the next time .

you have function named `getActualSleepHours`:

``````const getActualSleepHours = () => {
getSleepHours('Monday') + getSleepHours('Tuesday') + getSleepHours('Wednesday') + getSleepHours('Thursday') + getSleepHours('Friday') + getSleepHours('Saturday') + getSleepHours('Sunday');
return getSleepHours();
};
``````

which returns undefined, which isn’t right. What should it return?

format:

How do I format code in my posts?

It should return the amount of const getSleepHours (added 42).

but as you can see when you insert the console.log, this is not happening

maybe insert another console.log before the return to see the value of `getSleepHours()`? (which you are returning, to see its value)

I agree. The output is undefined. But I don’t understand what to do now.
It is exasperating ! I am stuck !!
What is the reason that the code didn’t add the values of getSleepHours?

``````const getActualSleepHours = () => {
getSleepHours('Monday') + getSleepHours('Tuesday') + getSleepHours('Wednesday') + getSleepHours('Thursday') + getSleepHours('Friday') + getSleepHours('Saturday') + getSleepHours('Sunday');
// lets checkout the value you are returning
console.log(getSleepHours());
return getSleepHours();
};
``````

oops, you return the function itself, not the calculated number of hours of sleep

The returned value is undefined !!

`undefined You gotNaN hours of sleep. That is too less and you need more sleep, take a rest !! undefined`

There are two undefined and I don’t know where are they from? Do I have to write something else to get the right result ?? Which function do I have to call to get the added amount of getSleepHours?

you can insert strings to see which is which:

``````console.log("getActualSleepHours returns: " + getSleepHours());
``````

I know that it retrurns undefined but what I can I do to get the result ?? Please, help me out :).

as i have been trying to show you, `return getSleepHours();` is not the right thing to return. Its the function itself. You need to return the number hours of actual sleep.

You need to fix you getActualSleepHours function to return the right result

This works better but…

``````const getSleepHours = (day) => {
switch (day) {
case 'Monday':
return 6;
break;
case 'Tuesday':
return 5.5;
break;
case 'Wednesday':
return 7;
break;
case 'Thursday':
return 6.5;
break;
case 'Friday':
return 7;
break;
case 'Saturday':
return 5;
break;
case 'Sunday':
return 5;
break;
}
};

const getActualSleepHours = () => {
return getSleepHours('Monday') + getSleepHours('Tuesday') + getSleepHours('Wednesday') + getSleepHours('Thursday') + getSleepHours('Friday') + getSleepHours('Saturday') + getSleepHours('Sunday');
};

const getIdealSleepHours = () => {
let idealHours = 6;
return idealHours * 7;
};

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
if(actualSleepHours === idealSleepHours) {
console.log('You got the right amount of sleep !!');
} else if(actualSleepHours > idealSleepHours) {
console.log('You got ' + (actualSleepHours - idealSleepHours) + ' hours more of sleep than needed.');
} else (actualSleepHours < idealSleepHours)
console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hours of sleep. That is too less and you need more sleep, take a rest !!');
};

console.log(calculateSleepDebt());
``````

this is the output now…
You got 1 hours more of sleep than needed.
You got -1 hours of sleep. That is too less and you need more sleep, take a rest !!
undefined

Where did I get the undefined from ?
And why did I get the output of else block ??

function always have to return a value, so if you don’t use a return keyword a function will return undefined by default. Given your calculateSleepDebt doesn’t have a return keyword, it will return the default, and by calling console.log on the function call you will log the returned result

else can’t have a condition since its everything else. Also, the curly brackets are missing for `else`

But when I put `curly brace` into the `else block` I get an error message like this…

Code with curly braces

``````const getSleepHours = (day) => {
switch (day) {
case 'Monday':
return 6;
break;
case 'Tuesday':
return 5.5;
break;
case 'Wednesday':
return 7;
break;
case 'Thursday':
return 6.5;
break;
case 'Friday':
return 7;
break;
case 'Saturday':
return 5;
break;
case 'Sunday':
return 6;
break;
}
};

const getActualSleepHours = () => {
return getSleepHours('Monday') + getSleepHours('Tuesday') + getSleepHours('Wednesday') + getSleepHours('Thursday') + getSleepHours('Friday') + getSleepHours('Saturday') + getSleepHours('Sunday');
};

const getIdealSleepHours = () => {
let idealHours = 6;
return idealHours * 7;
};

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
if(actualSleepHours === idealSleepHours) {
console.log('You got the right amount of sleep !!');
} else if(actualSleepHours > idealSleepHours) {
console.log('You got ' + (actualSleepHours - idealSleepHours) + ' hours more of sleep than needed.');
} else (actualSleepHours < idealSleepHours){
console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hours of sleep. That is too less and you need more sleep, take a rest !!');
}
};

console.log(calculateSleepDebt());

``````

error message

``````/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:43
} else (actualSleepHours < idealSleepHours){
^
SyntaxError: Unexpected token {
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
``````

and not the other half:

I’ll do my best man, I am from Germany ^^. But I will do my best

sehr gut. But i don’t see the relevance? English isn’t my native language either. Ich bin auch nicht deutsch, ich spreche ein bitsche deutsch. I could just have used google translate, but i swear i didn’t

1 Like

Finally

``````const getSleepHours = (day) => {
switch (day) {
case 'Monday':
return 6;
break;
case 'Tuesday':
return 5.5;
break;
case 'Wednesday':
return 7;
break;
case 'Thursday':
return 6.5;
break;
case 'Friday':
return 7;
break;
case 'Saturday':
return 5;
break;
case 'Sunday':
return 6;
break;
}
};

const getActualSleepHours = () => {
return getSleepHours('Monday') + getSleepHours('Tuesday') + getSleepHours('Wednesday') + getSleepHours('Thursday') + getSleepHours('Friday') + getSleepHours('Saturday') + getSleepHours('Sunday');
};

const getIdealSleepHours = () => {
let idealHours = 6;
return idealHours * 7;
};

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();

if(actualSleepHours === idealSleepHours) {
console.log('You got the right amount of sleep !!');
} else if(actualSleepHours > idealSleepHours) {
console.log('You got ' + (actualSleepHours - idealSleepHours) + ' hour(s) more of sleep than needed.');
} else if(actualSleepHours < idealSleepHours)
console.log('You got minus ' + (idealSleepHours - actualSleepHours) + ' hours of sleep. That is not enough and you need more sleep, take a rest !!');

};

console.log(calculateSleepDebt());

``````