Sleep debt Calculator

I rlly need help on this project, when I output , it’s always the same, pls help?

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

//console.log(getSleepHours(‘tuesday’));

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

const getIdealSleepHours = () => {
idealHours = 7;
idealHours * 7;

};

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
if(actualSleepHours === idealSleepHours) {
console.log(‘You slept 0 hours(s) less or more sleep than you need to this week, keep the good work up!’)
} else if (actualSleepHours < idealSleepHours) {
console.log(‘You got ’ + (idealSleepHours - actualSleepHours) + ’ hour(s) less sleep than you need this week. Get some rest’)
}

};

if (getActualSleepHours === getIdealSleepHours) {
console.log(‘Good job’);
} else if (getActualSleepHours > getIdealSleepHours) {
} else if (getActualSleepHours < getIdealSleepHours){
}

calculateSleepDebt();

if (getActualSleepHours < getIdealSleepHours) {
console.log(‘You got ’ + (getIdealSleepHours - getActualSleepHours) + ’ hour(s) less sleep than you needed this week. Get some rest.’);
} else if (getActualSleepHours > getIdealSleepHours) {
console.log(‘You got ’ + ( getActualSleepHours - getIdealSleepHours) + ’ hour(s) more sleep than you needed this week. Get up earlier next time.’);
} else if (getActualSleepHours === getIdealSleepHours)
{
console.log(‘You got the perfect amount of sleep!’)
}

console.log('Actual Sleep hours : ’ + getActualSleepHours())
console.log('Ideal Sleep hours : ’ + idealHours * 7)

Hey fang_square!

Next time you post code please write three backticks ````` before and after the code block in order to style formatting it and making it more visible. It makes it easier to read and help you debug it.

``````const getSleepHours = day => {
if (day === ‘monday’) {
return 6;
} else if (day === ‘tuesday’){
return 6;
} else if (day === ‘wednesday’){
return 7;
} else if (day === ‘thursday’){
return 7;
} else if (day === ‘friday’){
return 6;
} else if (day === ‘saturday’){
return 9;
} else if (day === ‘sunday’){
return 9;
}
};

//console.log(getSleepHours(‘tuesday’));

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

const getIdealSleepHours = () => {
idealHours = 7;
idealHours * 7;

};

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
if(actualSleepHours === idealSleepHours) {
console.log(‘You slept 0 hours(s) less or more sleep than you need to this week, keep the good work up!’)
} else if (actualSleepHours < idealSleepHours) {
console.log(‘You got ’ + (idealSleepHours - actualSleepHours) + ’ hour(s) less sleep than you need this week. Get some rest’)
}

};

if (getActualSleepHours === getIdealSleepHours) {
console.log(‘Good job’);
} else if (getActualSleepHours > getIdealSleepHours) {
} else if (getActualSleepHours < getIdealSleepHours){
}

calculateSleepDebt();

if (getActualSleepHours < getIdealSleepHours) {
console.log(‘You got ’ + (getIdealSleepHours - getActualSleepHours) + ’ hour(s) less sleep than you needed this week. Get some rest.’);
} else if (getActualSleepHours > getIdealSleepHours) {
console.log(‘You got ’ + ( getActualSleepHours - getIdealSleepHours) + ’ hour(s) more sleep than you needed this week. Get up earlier next time.’);
} else if (getActualSleepHours === getIdealSleepHours)
{
console.log(‘You got the perfect amount of sleep!’)
}

console.log('Actual Sleep hours : ’ + getActualSleepHours())
console.log('Ideal Sleep hours : ’ + idealHours * 7)
``````

In the whole code there’s a syntax error, you’re using some weird quotes to pass in a parameter or evaluate conditionals. E.g.

`//console.log(getSleepHours(‘tuesday’));`

You will need to use different quotes if you want this code to work, either single quotes like these `'` or double quotes like these `"`.

Does this help solve your issue?

I also took a very quick look because the code is hard to read due to the lack of formatting. But this function doesn’t do anything:

`idealHours` is not defined, and the function neither prints, pushes or returns anything.
There may be more issues.

@usernamegiapreso the quotemark issue is due to the lack of formatting. When code is pasted in without being marked as such, they are converted. They may be right in the original code.

1 Like

Thanks, I’ll try and fix it when I have time, first I gotta go to school. Next time, I’ll also do the ``` thing so it easier to check it. Thanks again!

1 Like

I fixed this part of the code but it keeps saying you the same thing even if I change the number of hours of sleep.

``````const getSleepHours = day => {
if (day === 'monday') {
return 7;
} else if (day === 'tuesday'){
return 7;
} else if (day === 'wednesday'){
return 7;
} else if (day === 'thursday'){
return 7;
} else if (day === 'friday'){
return 7;
} else if (day === 'saturday'){
return 7;
} else if (day === 'sunday'){
return 7;
}
}

console.log(getSleepHours('tuesday'));

const getActualSleepHours = () => getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('friday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday');

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

};

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

if (getActualSleepHours === getIdealSleepHours) {
console.log('Good job');
} else if (getActualSleepHours > getIdealSleepHours) {
console.log('You slept to much');
} else if (getActualSleepHours < getIdealSleepHours){
console.log('Get some rest');
}

console.log('Actual sleep hours is : ' + getActualSleepHours());
console.log('Ideal sleep hours is : ' + getIdealSleepHours());

calculateSleepDebt();

``````

I added a bit of unnecessary code to help me know if the code was printing out the right response. (I hope I did the correct formatting)

You did good!
So, let’s review it together

In here we define a function with arrow declaration that should return the number of hours we plan to sleep throughout the week. In your case whatever is the day the function is always going to return 7:

``````const getSleepHours = day => {
if (day === 'monday') {
return 7;
} else if (day === 'tuesday'){
return 7;
} else if (day === 'wednesday'){
return 7;
} else if (day === 'thursday'){
return 7;
} else if (day === 'friday'){
return 7;
} else if (day === 'saturday'){
return 7;
} else if (day === 'sunday'){
return 7;
}
}
console.log(getSleepHours('tuesday'));
//ouput is: 7

``````

The function `getActualSleepHours()` is also an arrow function and is going to return the sum of 49, as the sum of the daily hours that you actually have slept

``````const getActualSleepHours = () => getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('friday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday');

console.log(getActualSleepHours());
//output is: 49
``````

Now we got the function `getIdealSleepHours()`, you declared this function with arrow declaration and you defined inside of it the ideal amount of hours that should be slept. This function is going to return the ideal amount of weekly sleep hours, in your case again 49

``````const getIdealSleepHours = () => {
let idealHours = 7;
return idealHours * 7;
};
console.log(getIdealSleepHours());
//output is: 49
``````

With the arrow function `calculateSleepDebt()` you want to determine the difference between hours you should have slept and hours you actually slept. However in this function you forgot to actually calculate this difference and it is also not returning anything nor printing anything to the browser console. So it’s doing nothing (just entropy).

``````const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
}
console.log(calculateSleepDebt());
//output is: undefined
``````

The following piece of code cannot access the variables created inside the previous function, which is what I think you were trying to do. What this code will do is to compare if the two functions (not their results, but the actual functions) are equal or different. Interestingly enough in the way JS compares the functions `getActualSleepHours` is less than `getIdealSleepHours`.

Now this answers also your question about why this code always print the same thing. It does so because it’s comparing two things (functions) that never change.

`````` if (getActualSleepHours === getIdealSleepHours) {
console.log('Good job');
} else if (getActualSleepHours > getIdealSleepHours) {
console.log('You slept to much');
} else if (getActualSleepHours < getIdealSleepHours){
console.log('Get some rest');
}
``````

Does this help?

`` Thanks for you time and effort for helping me, I've added you corrections to my code and tinkered with it a bit and I have succeeded! I want to thank you for that reply becuase it sure was detailed and helpful. :grinning:``
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.