Help with Sleep Debt Calculator

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

Try again. I pasted your original code above in case you don’t have it anymore because you were closer to the answer with it.

I think the line breaks are making you think they are different, Based on your formatting, look at these examples. Either one of these will return 7:

``````const addSevenOnesTogether = () => {
return 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1;
}
``````
``````const addSevenOnesTogether = () => {
let sum = 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1;
return sum;
}
``````

Where did your extra `getSleepHours()` come from?

according to your diagram let sum = 1 contains a value that is 1. So therefore let sum must contain a value?

I used this formatting because of how you are formatting your code

``````  let sum = 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1;
``````

It’s actually just

``````  let sum = 1 + 1 + 1 + 1 + 1 + 1 + 1;
``````

Your code is already adding the 7 values together here

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

What you’re trying to do is store that value so you can return it. You don’t need extra function calls.

Do I need Sum, or is just let OK

The elephant in the room is `break`. Why has nobody pointed that out, just to get it out of the way and shorten the source listings?

And, if we’re going to be repeatedly calling a function, can we not find a more concise way to do it?

``````  let hours = 0
for (const day of ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']) {
hours += getSleepHours(day)
}
return hours
}
``````

I thought for was only used in loops, its just i’m doing a level three project… and I’m trying to get it to process, the same as the video does…

if for a Variable…

Yes, and? We’ve used a loop to confine having to type the function call to a single statement, within the loop.

Personally, it is preferable to do these challenges without the aid of a video, and just follow the instructions as given. The video is a last resort, not a first step.

OK so delete the get sleep hours variable… and apply the rewrite!

Gimme a sec… I don’t want to answer that. For our purposes, and for the sake of sanity, swap in the code snippet above for the code you have in `getActualSleepHours()`. Run it, and report back any error messages, along with that version of the full code.

No screenshots! Post raw, formatted code, please.

``````const getSleepHours = (day) => {

switch (day) {
case "Monday":
return 6;
break;
case "Tuesday":
return 6;
break;
case "Wednesday":
return 7;
break;
case "Thursday":
return 7;
break;
case "Friday":
return 8;
break;
case "Saturday":
return 7;
break;
case "Sunday":
return 9;
break;

default:
return "Error."

}
};

//console.log(getSleepHours("Sunday"));

const getActualSleepHours = () => {
let hours = 0
for (const day of ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']) {
hours += getSleepHours(day)
}
return hours
}

const getIdealSleepHours = () => {//NewFunctionNme
let idealHours = 7// Created New Variable.
return idealHours * 7;// Aplied Maths to it.
};

const calculateSleepDebt = () => {//L" NewV
let actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
// const idealSleepHours also nV

if(actualSleepHours === idealSleepHours){
console.log("You Done Good.")
}
}; //, This is the Closing Bracket Related to the Original Function. This else statment will get evaluated if none of the previous conditions are true.

console.log(calculateSleepDebt());
``````
``````const getSleepHours = (day) => {

switch (day) {
case "Monday":
return 6;
break;
case "Tuesday":
return 6;
break;
case "Wednesday":
return 7;
break;
case "Thursday":
return 7;
break;
case "Friday":
return 8;
break;
case "Saturday":
return 7;
break;
case "Sunday":
return 9;
break;

default:
return "Error."

}
};

//console.log(getSleepHours("Sunday"));

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

const getIdealSleepHours = () => {//NewFunctionNme
let idealHours = 7// Created New Variable.
return idealHours * 7;// Aplied Maths to it.
};

const calculateSleepDebt = () => {//L" NewV
let actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
// const idealSleepHours also nV

if(actualSleepHours === idealSleepHours){
console.log("You Done Good.")
}
}; //, This is the Closing Bracket Related to the Original Function. This else statment will get evaluated if none of the previous conditions are true.

``````
``````const getSleepHours = (day) => {
switch (day) {
case "Monday": return 6;
case "Tuesday": return 6;
case "Wednesday": return 7;
case "Thursday": return 7;
case "Friday": return 8;
case "Saturday": return 7;
case "Sunday": return 9;
default: return "Error."
}
};
``````

Try that, along with,

``````const getActualSleepHours = () => {
let hours = 0
for (const day of ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']) {
hours += getSleepHours(day)
}
return hours
}
console.log(getActualSleepHours())
``````
``````50
``````

That means we’re getting a number, the week’s total. Now we hand that to the final logic that tells us whether we’re getting the right amount of sleep.

yes i’ll try that. But what else I have to do is turn ever number in that code to a 7, because the I’m trying to get the actual sleep to match ideal sleep , to make the very last function calculateSleepDebt actually function…

Now, given that we are supplying the names do we need to error check? Consider,

``````const getSleepHours = (day) => {
return {
Monday: 6,
Tuesday: 6,
Wednesday: 7,
Thursday: 7,
Friday: 8,
Saturday: 7,
Sunday: 9
}[day]
}
``````

That does what your switch does, but with no logic, only lookup.

Trying to Prevent this UNDEFINED Message. Day was in the Original Parameter,

The function has no return statement so undefined is the default return. If you return the value, and log it at the caller, this will go away.