Problem in JavaScript’s functions project (Sleep debt calculator)

This is simple task yet cant work it out. it says
const getSleepHours = day => { ^RangeError: Maximum call stack size exceeded at getSleepHours

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

console.log(getSleepHours(‘Monday’));
console.log(getSleepHours(‘Thursday’));
console.log(getSleepHours(‘Sunday’));

const getActualSleepHours = () => {getSleepHours(‘Monday’) + getSleepHours(‘Tuesday’) + getSleepHours(‘Wednesday’) +
getSleepHours(‘Thrusday’) + getSleepHours(‘Friday’) + getSleepHours(‘Saturday’) + getSleepHours(‘Sunday’);
return getActualSleepHours();
};

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

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

When the function getActualSleepHours is called, you end up in an infinite recursion. Once this function is executed, you make calls to getSleepHours for each day of the week and add the results. But, you neither return the result nor store it in a variable. Your last statement is return getActualSleepHours(); which means you will call getActualSleepHours again. You will end up in an endless loop with the function calling itself over and over.

1 Like

Sorted. Thank you very much!

1 Like