Sleep dept calulator

Hi I was wondering if anyone could help me as I am getting an error with my code saying that the function does not exist and I do not know why?

/Function created for hours of sleep per day.

const getSleepHours = day => {

  if (day === 'monday'){

    return 8;

  } else if (day === 'tuesday'){

    return 8;

  } else if (day === 'wednesday'){

    return 8;

  } else if (day === 'thursday'){

    return 8;

  } else if (day === 'friday'){

    return 8;

  } else if (day === 'saturday'){

    return 8;

  } else if (day === 'sunday'){

    return 8;

  }

}

//Function of total hours slept.

const getActualSleepHours = () => {

  

  getSleepHours('monday') + 

  getSleepHours('tuesday') + 

  getSleepHours('wednesday') + 

  getSleepHours('thursday') + 

  getSleepHours('friday') + 

  getSleepHours('saturday') + 

  getSleepHours('sunday');

  

}

//Function - ideal sleep hours.  

const getIdealSleepHours = () => {

  const idealHours = 8;

  return idealHours * 7;  

}

// Function - Calculate the sleep dept.

let calculateSleepDept = () => {

  let actualSleepHours = getActualSleepHours();

  let idealSleepHours = getIdealSleepHours();

//Working if person has perfect, less or more sleep.

    if (actualSleepHours === idealSleepHours) {

    console.log ('You got the perfect amount of sleep');

  } else if (actualSleepHours > idealSleepHours) {

      console.log ('You got ' + (idealSleepHours + actualSleepHours) + ' more sleep than you wanted');

  } else {

      console.log ('You got ' + (idealSleepHours + actualSleepHours) + ' less sleep than you wanted');

    } 

}

// call function of sleep debt.

calculateSleepDebt();

The function seem to exists, the scope appears to be right. But if we then compare the deceleration:

let calculateSleepDept = () => {

with the call:

calculateSleepDebt();

I see a typo. You see it to?

Thank you. I can see my mistake now. For some reason the function is logging “You got NaN less sleep than you wanted”. I thought I would get a number.

So the error you had is resolved? And now you have a new error/problem:

This error can happen because your provide a non-valid day. What you could do is add an else clause to getSleepHours:

  } else if (day === 'sunday'){

    return 8;

  } else { 
    console.log(`${day} is not a valid day`);
}

this should help to narrow down the problem

but these thinking steps I did above, is what you should teach yourself :slight_smile: I have been programming for 5+ years, and I still make mistakes. Being able to debug is very important :slight_smile:

Thank you again. I have only really starting programming over the last month.

I fixed the problem and got the solution. I ended up creating a variable for the total of getSleepHours.

Are you currently working in the industry?

Thanks again for your help. It was my first time asking for help, so it is good to know that I have somewhere to ask questions when I need too.

1 Like

you’re welcome :slight_smile: which is why I advising that focusing on being able to solve errors is also very important and teaching yourself these thinking steps: What could be wrong? How could I verify what could go wrong?

Not discussing personal matters out in the open on a public forum. You could send me a direct/personal messages

Please make new topics when questions arise. Then other people can also help you, you don’t want to get your advise from one person all the time. Different developers/people could lead to different answers and insights.

Don’t get me wrong, I like to answer questions, but you must not trap yourself to only rely on a single source.

1 Like