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’;
}
};
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:
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.
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.
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!";
}
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.