Can't get console.log to work on javascript sleep calculator

here is my code and I can’t seem to get the console.log to work I just get errrors everytime, it worked when I tested first part of code but can’t get it to work for whole project. Any help would be greatly appreciated.

const getSleepHours = day => {
switch (day) {
case ‘monday’:
return 8;
case ‘tuesday’:
return 6;
case ‘wednesday’:
return 7;
case ‘thursday’:
return 8;
case ‘friday’:
return 5;
case ‘saturday’:
return 6;
case ‘sunday’:
return 7;
}
};

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

{
const getActualSleepHours = () =>
getSleepHours(‘monday’) + getSleepHours(‘tuesday’) + getSleepHours(‘wednesday’) + getSleepHours(‘thursday’) + getSleepHours(‘Friday’) + getSleepHours(‘saturday’) + getSleepHours(‘sunday’);
};

const calculateSleepDebt = () => {
let actualsleephours = getActualSleepHours();
let idealSleepHours = getIdealSleepHours();
};

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

When you share code you’ll want to make sure that you’re providing an exact copy so that other people can run it and get the same thing to happen. So watch out with how forums and other things format and display, and see if there isn’t a way that they can be asked to keep your code intact.

It sounds like you’re saying that you wrote a whole bunch of code and then can’t run it.
How about writing only a very small piece of code, testing it, and then adding on a bit more, testing that, and so on, until you’re done? Writing code is an interactive process, not an all-at-once thing.
If getActualSleepHours isn’t behaving as it should, then perhaps you might look at the individual things that it is doing, do those individually and see if those behave like you expect, and see if you’re able to combine those results into what getActualSleepHours should have responded with.

1 Like

Have you read the error message? What does it tell you?

Here is error code, I ran a console.log below all the days and they all come back correct when I run them.

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:34
console.log(getActualSleepHours());
^

ReferenceError: getActualSleepHours is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:34:13)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

The problem is a small hard to see typo. Your opening curly bracket is before the start of your function declaration

{ //this curly bracket
const getActualSleepHours = () => //should be here
getSleepHours(‘monday’) + getSleepHours(‘tuesday’) + getSleepHours(‘wednesday’) + getSleepHours(‘thursday’) + getSleepHours(‘Friday’) + getSleepHours(‘saturday’) + getSleepHours(‘sunday’);
};

Hope this helps. :grin:

Thank you for your help, it fixed the problem.

Yeah we saw that, but @ionatan and I were hoping for @snake1oz to find that out by himself by reading the error message and testing the functionality.

You shouldn’t always deliver the solution on a silver platter. Coding is all about trial and error.

Happy coding!

2 Likes

What can you derive from the error message?
It says getActualSleepHours() is undefined so there must be something wrong there.

Which problem? Moving the curly brace corrects the reference error, but when I run your code (which takes extra work since the forum has applied undesired formatting to it - please see How do I format code in my posts?) I get undefined printed to the console when this line is executed:

1 Like