What am I getting wrong here? Sleep Debt Calculator - Javascript

Of course! Just because it’s a function doesn’t mean it halts the running of everything else.

The block inside the function. This is where all the work that change the inputs to the outputs happens. It’s what’s necessary for the function to work.

(To be honest, this is the first time someone has critically analysed my code, and it’s fun to see where one can improve and learn from his own mistakes. Thanks very much for that, ionatan.)

You have a single point of execution which moves through your code. Like a marble rolling downhill.

When this moves into a function, the things in the function get carried out. Afterwards, this point moves back to the caller. There is now a result. No other code ran, the point of execution was in the function.

When does this point of execution go through whatever is outside the function? That’s unrelated to when the function runs, this is a different location. It probably doesn’t run as many times.

Whatever is outside your function is unrelated to your function. That’s something else. If you’re writing a function, then that’s irrelevant.

Whatever code your function needs to run, put it in the function. If you need it to run during the function, it goes into the function, that’s what runs when the function runs.

So, after the point of execution moves to the function caller, does it continue to the code that is outside the function, in between the function declaration and the function caller?

After the function call? Would it jump back to the place where code unrelated to the function started?

Understand this.

Understand this as well.

no why would it. you finished calling the function, do the next thing.
(also that would be completely uncontrollable)

point at the top of your file and start executing things. when you reach the bottom, the program ends.

You’re suggesting that this:

const f = () => {
}
f()
f()

would be an infinite loop, because after calling f, f would get called.

create f
run f
run f
exit

1 Like
const areaOfRectangle = (width, length) => {
    return width * length;
}

console.log('howdy folks, its ya boi');

console.log(areaOfRectangle(4, 10);

What gets logged to the terminal first, 40 or howdy folks, its ya boi?

according to you it would say howdy both before and after 40, so there’s your proof that the outside doesn’t run along with the function

which is the point of being a self-contained unit. the unit runs.

1 Like

Of course, because the outside code doesn’t run with the function

so therefore you can’t put code that belongs to your function outside it

1 Like

Alright, I understand now. Thanks for the help! I’ll come to you if I have any further questions.