Sleep Debt Calculator & Functions

Hi everyone. I’m working on the sleep debt calculator challenge in the intro to JavaScript module. I’m working through the project ok but I have a question about functions. For the project, I have written two different functions (getActualSleep & getIdealSleep) Each function calculates ideal sleep & actual sleep over the course of the week. I understand this no problem. From there I am writing another function that compares these two functions to determine if the subject is getting enough sleep throughout the course of the week. Herein lies my question. Before I compare the two different week totals I had to write a function that converts getActualSleepHours to a new variable actualSleepHours and a function that converts getIdealSleepHours to a new variable idealSleepHours. Why is this conversion necessary? Why can I not simply call the two functions in my function that compares their values. Why is the new variable necessary. Please let me know if my question is not clear so I can add code to this post to clarify. As always, thank you to the community for your help!

Notice that getActualSleepHours is a function, whereas actualSleepHours is a number, so those variables are not identical.
The actualSleepHours variable contains the result of calling the function getActualSleepHours

Yes, the project can be done without creating those additional variables.
But, assuming that you’ll be using that number again, it may be more convenient [and it may save some processing] to have the number stored as a variable, instead of having a function recompute it each time.

Thank you, that is clear. Does this mean that once a function is written the computer stores the result in its memory so that it does not need to recompute the function every time it is called?

no, a function runs (and recomputes stuff) every time it is called;
the results of functions are not stored
unless you assign the result to a variable or something

Ok, thank you, understood.

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