Sleep Debt Calculator – My code works, but it is saying undefined

This is the output:

You haven’t slept enough. You should get 5 hours more rest.
undefined

Does anyone know why the code below reads “undefined”?

const getSleepHours = day => {
day = day.toLowerCase();
switch (day) {
case ‘sunday’:
return 6
break;
case ‘monday’:
return 5
break;
case ‘tuesday’:
return 6.5
break;
case ‘wednesday’:
return 4.5
break;
case ‘thursday’:
return 6
break;
case ‘friday’:
return 7
break;
case ‘saturday’:
return 5.5
break;
default:
return; ‘Error!’;
break;
}
};

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

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

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
if (actualSleepHours === idealSleepHours) {
console.log(‘You got the perfect amount of sleep!’);
} else if (actualSleepHours > idealSleepHours) {
console.log(‘You got ‘+(actualSleepHours - idealSleepHours)+’ more sleep than you needed.’);
} else if (actualSleepHours < idealSleepHours) {
console.log(‘You haven’t slept enough. You should get ‘+(idealSleepHours - actualSleepHours)+’ hours more rest.’);
}
}
console.log(calculateSleepDebt());

Please remember to format your code.

const getSleepHours = day => {
    day = day.toLowerCase();
    switch (day) {
        case‘ sunday’:
            return 6
            break;
        case‘ monday’:
            return 5
            break;
        case‘ tuesday’:
            return 6.5
            break;
        case‘ wednesday’:
            return 4.5
            break;
        case‘ thursday’:
            return 6
            break;
        case‘ friday’:
            return 7
            break;
        case‘ saturday’:
            return 5.5
            break;
        default:
            return;‘
            Error!’;
            break;
    }
};

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

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

const calculateSleepDebt = () => {
    const actualSleepHours = getActualSleepHours();
    const idealSleepHours = getIdealSleepHours();
    if (actualSleepHours === idealSleepHours) {
        console.log(‘You got the perfect amount of sleep!’);
    } else if (actualSleepHours > idealSleepHours) {
        console.log(‘You got‘ + (actualSleepHours - idealSleepHours) + ’more sleep than you needed.’);
    } else if (actualSleepHours < idealSleepHours) {
        console.log(‘You haven’ t slept enough.You should get‘ + (idealSleepHours - actualSleepHours) + ’hours more rest.’);
    }
}
console.log(calculateSleepDebt());

There are a good number of syntax issues in your switch statement: Resolve those, and you will be able to better analyze your functionality.


The last line of your code logs the output of your calculateSleepDebt function… which is nothing: In that function, you log the results instead of returning them. Change your console.log() calls inside of calculateSleepDebt to return calls.

const calculateSleepDebt = () => {
    const actualSleepHours = getActualSleepHours();
    const idealSleepHours = getIdealSleepHours();
    if (actualSleepHours === idealSleepHours) {
        return('You got the perfect amount of sleep!');
    } else if (actualSleepHours > idealSleepHours) {
        return('You got' + (actualSleepHours - idealSleepHours) + 'more sleep than you needed.');
    } else if (actualSleepHours < idealSleepHours) {
        return("You haven't slept enough. You should get " + (idealSleepHours - actualSleepHours) + " hours more rest.");
    }
}
console.log(calculateSleepDebt());
1 Like