Sleep debt calculator

Hey,
i cant seem to get this one right. i’m stuck with an error that i can’t seem to resolve.
“ReferenceError: getActualSleepHours is not defined”. can anyone spot my mistake?

const getSleepHours = day => {
if (day === ‘monday’) {
return 8;
} else if (day === ‘tuesday’) {
return 8;
} else if (day === ‘wednesday’) {
return 8;
} else if (day === ‘thursday’) {
return 8;
} else if (day === ‘friday’) {
return 8;
} else if (day === ‘saturday’) {
return 8;
} else if (day === ‘sunday’) {
return 8;
} else {
return ‘0’;
}
};

let actualSleepHours = getActualSleepHours;
const getActualSleepHours = () => getSleepHours(‘monday’) + getSleepHours(‘tuesday’) + getSleepHours(‘wednesday’) + getSleepHours(‘thursday’) + getSleepHours(‘friday’) + getSleepHours(‘saturday’) + getSleepHours(‘sunday’);

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

console.log(getIdealSleepHours());
console.log(getActualSleepHours());

const calculateSleepDebt = () => {
let actualSleepHours = getActualSleepHours();
let idealSleepHours = getIdealSleepHours();
}
if (actualSleepHours === idealSleepHours) {
console.log(‘perfect sleep’)
} else if (actualSleepHours > idealSleepHours) {
console.log(‘you got’ + (idealSleepHours - actualSleepHours) + ‘hours more sleep than desired’)
} else {
console.log(‘you only got’ + (actualSleepHours) + ‘you should get some rest’)
}

1 Like

Hi, welcome to the forums! To get the best help possible please format your code using this:

Formatting Code

By wrapping a block of code in triple backticks on a new line before and after, your code’s indentation and any HTML tags in it will be preserved so they’re visible. Not only that, but you get cool syntax highlighting:


And please post a link to the exercise!
Try posting this in the #get-help section as well!

1 Like

Looking at your code the problem is here:

let actualSleepHours = getActualSleepHours;
const getActualSleepHours = () => getSleepHours(‘monday’) + getSleepHours(‘tuesday’) + getSleepHours(‘wednesday’) + getSleepHours(‘thursday’) + getSleepHours(‘friday’) + getSleepHours(‘saturday’) + getSleepHours(‘sunday’);

and here:

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

Notice how you assigned let actualSleepHours = getActualSleepHours; and let idealSleepHours = getIdealSleepHours; before you actually defined the functions getActualSleepHours and getIdealSleepHours? That’s why it’s throwing the error.
Now if you move those statements under said functions, no error is thrown. :smile:

I just noticed something, you made these statements: let actualSleepHours = getActualSleepHours; and let idealSleepHours = getIdealSleepHours;, but without the parenthesis after the function, it won’t save the value of the function to the variable you defined.

2 Likes

An interesting element of JavaScript which is not shared by many other languages:

In JavaScript, there a system known as hoisting, in which a part of the program cab access the variables in a function that is declared later in the program:

let varA = someFunction();
console.log(varA);
function someFunction(){
    return "Hello!";
}

Will print:

>>Hello!

This is, however, considered bad practise

Why is this considered bad practice?

1 Like

Just due to readability. Imagine having a larger program, and at line 2 you declare a variable called someVar and set it equal to someFunction(), but someFunction() was declared on line 50. This is valid syntax, but it wouldn’t be very easy to debug.

2 Likes

Thank you so much!
It works now :smiley:

1 Like