Sleep Debt Calculator Project

I’m stuck on Step 9 of this project:

Now that you have actualSleepHours and idealSleepHours , you can write a few if / else statements to output the result to the console. The function should fulfill this logic:

  • If actual sleep equals ideal sleep, log to the console that the user got the perfect amount of sleep.
  • If the actual sleep is greater than the ideal sleep, log to the console that the user got more sleep than needed.
  • If the actual sleep is less than the ideal sleep, log to the console that the user should get some rest.

I’ve followed it exactly to how it’s shown in the walk through video, but I’m still getting this error with my if/else statement:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:61
};
^
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.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)

For this part of the code:

if (actualSleepHours === idealSleepHours) {
console.log(‘You got the perfect amount of sleep’);
} else if (actualSleepHours > idealSleepHours) {
console.log(‘You got more sleep than needed’);
} else if (actualSleepHours < idealSleepHours) {
console.log(‘You should get some rest’);
} else {
console.log(‘Error! Something went wrong,check your code.’);
}
};

It’s the last }; which the error is referring to. However when I delete the ; I get this error:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:86
});
^
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.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)

Can anyone help as I’m struggling - thanks!

make sure things match up…

how so? I’ve checked over all the curly braces and am still getting the same error

You could temporarily delete/comment out and add things back one small part at a time.
If you run your code between edits then you won’t have to fix things you did long ago and no longer remember.
Consistency in formatting will make mismatched things stand out as well, inserting/removing things in pairs, indentation matching etc.

how did you do this?

okay so for instance I’ve done that and just kept in the first line:

if (actualSleepHours === idealSleepHours) {
console.log(‘You got the perfect amount of sleep.’);
}

but now this error comes up:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:52
if (actualSleepHours === idealSleepHours) {
^

ReferenceError: actualSleepHours is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:52:5)
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

And… that describes a problem that you could adjust for. Right? Not sure what you’re asking. Read what the error message says

Those three lines are not the first things your program should do, doing that before anything else doesn’t make sense, like the error message points out.

1 Like

This is the code that comes before that:

const getSleepHours = day => {

switch(day) {
case ‘monday’:
return 8
break;
case ‘tuesday’:
return 7
break;
case ‘wednesday’:
return 7
break;
case ‘thursday’:
return 9
break;
case ‘friday’:
return 10
break;
case ‘saturday’:
return 10
break;
case ‘sunday’:
return 6
break;
default:
return “Error!”
}
};

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

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

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

So why is “actualSleepHours” not defined when I have defined it in the previous step?

function f() {
    const x = 3
}

f()
const y = 5
console.log(y)  // that's fine.
console.log(x)  // no such x

so if actualSleepHours is not defined, but the instructions have asked to make actualSleepHours with zero parameters, should I add in a parameter for this step?

1 Like

If you want to communicate to or from a function, then that should in most cases happen through arguments or return values.
A function accepts some kind of question (otherwise it would just be a concrete value) and produces some kind of result.

You might also consider whether that information should have been in the function in the first place, or whether it should leave the function. What’s the purpose of the function? It should do that and only that, right. Don’t bolt on extra things if it’s not supposed to do that, just because there’s some other problem you’re solving.

So in this case, I have defined the values in the switch statement with the number of hours slept each day of the week.

The question for this function is the actual hours slept, which is the all the hours for each day of the week added together.

That’s it’s only purpose so I don’t understand how I could write the code any differently?

Your switch statement defines nothing, and it probably shouldn’t. Your getSleepHours function on the other hand returns some value. It’s using a switch to do that, but that’s not significant to anything outside it.

Your calculateSleepDebt sounds like it’s collecting information from other places and combining them.
But you wrote the combining outside your function, so you no longer see any of the variables there, they don’t even exist because that function has finished and has cleaned up.

If you’re writing a function that’s supposed to do some particular thing, then its code would need to be inside that function… not outside it.

Okay, I’ve fixed the getActualSleepHours and I’m no longer getting any syntax errors. This is what I’ve changed it to:

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

getActualSleepHours();

However when I go to call the program, nothing happens:

console.log(calculateSleepDebt());

yes you word but you haft to tell the @systemrunner42169 ok

how do we see your topic

Was it broken?
It definitely is now, if it returns itself. It probably shouldn’t return a function.

It should probably return the sum of the sleep hours of each day instead.

This is the total code for the project:

const getSleepHours = day => {

switch(day) {
case ‘monday’:
return 8
break;
case ‘tuesday’:
return 7
break;
case ‘wednesday’:
return 7
break;
case ‘thursday’:
return 9
break;
case ‘friday’:
return 10
break;
case ‘saturday’:
return 10
break;
case ‘sunday’:
return 6
break;
default:
return “Error!”
}
};

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

console.log(getSleepHours(‘monday’));
console.log(getActualSleepHours());

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

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

if (actualSleepHours === idealSleepHours) {
console.log(“You’ve got the perfect amount of sleep”);
} else if (actualSleepHours > idealSleepHours) {
console.log("You’ve got " + (actualSleepHours - idealSleepHours) + ’ hour(s) sleep than needed this week.’);
} else if (actualSleepHours < idealSleepHours) {
console.log(‘You got ’ + (idealSleepHours - actualSleepHours) + ’ hour(s) less sleep than you needed this week. Get some rest.’);
} else { console.log(‘Error! Something went wrong, check your code.’);

};

calculateSleepDebt();

However nothing is logging in the console.

the getActualSleepHours was coming up as undefined previously, so I added a return statement.

I’m guessing the problem is somewhere here, as the actualSleepHours is what keeps coming up as not defined:

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

It seems to me like you haven’t decided what that function should do, so start with that? And then when you have decided what your function should do, you could for example write comments in English before you start writing the code for it.

const myfunction = () => {
  // water the cat

  // walk the milk

  // pet the cactus

}

Then you can fill in the corresponding code.

Right? your function currently creates two variables and exits. That’s not useful, so you forgot what it is supposed to do, and you can’t fix it before you’ve decided that.

And before you do anything at all you might want to comment out the things that prevent your code from successfully running or otherwise isn’t doing its job. There should be no code that lacks purpose, if it’s not doing something then it’s probably not helpful to have it.

And every time you change anything at all… run it, and observe whether the change had the desired effect. Otherwise you’re writing code blindly. If you make a mistake but run it often, then when something doesn’t have the behaviour you meant, then you’ll notice immediately instead of after having written lots of other code and nothing works, that’s no good. Make partial progress, and verify each piece of progress.