Javascript Sleep Debt Project Question


#1

Hello Everyone!

My name is Manuel and I’m 100% new to programming, I don’t have an IT background whatsoever and I’ve only picked up computer knowledge from gaming on them and heavy web surfing. I’ve included my code below, I keep getting an error (below)

"if (actualSleepHours === idealSleepHours) {
^

ReferenceError: actualSleepHours is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:40:5)

I’ve included my code below this one, can someone please help with resolving what exactly I did wrong? I’m not one to ask for help but I just can’t figure this one out. Thank you in advance!

My code -
const getSleepHours = (day) => {
if(day === ‘Monday’) {
return 8;
} else if (day === ‘Tuesday’) {
return 8;
} else if (day === ‘Wednesday’) {
return 7;
} else if (day === ‘Thursday’) {
return 6;
} else if (day === ‘Friday’) {
return 9;
} else if (day === ‘Saturday’) {
return 9;
} else if (day === ‘Sunday’) {
return 8;
} else {

}
}

const getActualSleepHours = () =>
getSleepHours(‘Monday’) +
getSleepHours(‘Tuesday’) +
getSleepHours(‘Wednesday’) +
getSleepHours(‘Thursday’) +
getSleepHours(‘Friday’) +
getSleepHours(‘Saturday’) +
getSleepHours(‘Sunday’);

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

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

if (actualSleepHours === idealSleepHours) {
console.log(‘You are good on sleep, time to wake up’)
} else if (actualSleepHours < idealSleepHours) {
console.log(‘You need some more sleep, get to be early’)
} else if (actualSleepHours > idealSleepHours) {
console.log(’ You’ve had plenty of sleep, stay up a little later tonight’)
} else {
console.log(‘something went wrong, go back and try again!’);
};
console.log(calculateSleepDebt());


#2

Firstly the last if statement has to be part of a function:

You can add it like this:

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

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

    if (actualSleepHours === idealSleepHours) {
        console.log('You are good on sleep, time to wake up')
    } else if (actualSleepHours < idealSleepHours) {
        console.log('You need some more sleep, get to be early')
    } else if (actualSleepHours > idealSleepHours) {
        console.log(' You've had plenty of sleep, stay up a little later tonight')
    } else {
        console.log('something went wrong, go back and try again!');
    }
};

Second is this line of code:

When you have an apostrophe in a string (like with the word “You’ve”), you have to enclose the string in double quotes instead of single quotes:

console.log(" You've had plenty of sleep, stay up a little later tonight")

This should make your code work, but the another thing I want to mention is that it is good practice to end your lines with a semicolon “;” like this:

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

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

    if (actualSleepHours === idealSleepHours) {
        console.log('You are good on sleep, time to wake up');
    } else if (actualSleepHours < idealSleepHours) {
        console.log('You need some more sleep, get to be early');
    } else if (actualSleepHours > idealSleepHours) {
        console.log(' You've had plenty of sleep, stay up a little later tonight');
    } else {
        console.log('something went wrong, go back and try again!');
    }
};

Lastly your code will return a string for example You've had plenty of sleep, stay up a little later tonight, but it will also return undefined because your’re not assigning a value to the calculateSleepDebt constant, you’re just logging to the console.

To assign a value with the method you used, you have to return a value instead of logging it in the console:

const calculateSleepDebt = () => {
    const actualSleepHours = getActualSleepHours();
    const idealSleepHours = getIdealSleepHours();
    
    if (actualSleepHours === idealSleepHours) {
        return 'You are good on sleep, time to wake up';
    } else if (actualSleepHours < idealSleepHours) {
        return 'You need some more sleep, get to be early';
    } else if (actualSleepHours > idealSleepHours) {
        return " You've had plenty of sleep, stay up a little later tonight";
    } else {
        return 'something went wrong, go back and try again!';
    }
};
console.log(calculateSleepDebt());

#3

Thank you for the help!