Hi there, so here is how getSubTotal() knows the number of Pizzas ordered being 3:
First of all, don’t get confused by the name of the parameter within
the name “ItemCount” is just so you, the developer understand what it stands for. For all you care you could write something like
Now how does console.log(getSubtTotal(orderCount)); know the number 3?
The magic happens within your takeOrder function
it says orderCount++; which is the lazy way of writting orderCount = orderCount + 1;
The code above would work just the same and might be a little more descriptive about whats going on.
So every time you take an order (add a Pizza) and run the takeOrder function it changes the let orderCount = 0; by adding +1.
So the first time it runs it changes let orderCount = 0; to let orderCount = 1;
The second time it runs it changes let orderCount = 1; to let orderCount = 2;
The 3rd time it changes let orderCount = 2; to let orderCount = 3;
So because you have 3 Pizzas takeOrder() will run 3 times. Every time takeOrder() runs it will add +1 to the orderCount.
By the time the program is done taking Pizza orders and wants to get the sub total: console.log(getSubTotal(orderCount)); let orderCount no longer = 0; but let orderCount = 3;
So getSubTotal(orderCount)); uses the 3 as the parameter.
If the takeOrder function would run 5 times it would add +1 five times to let orderCount = 0; and 5+0=5 meaning getSubTotal(orderCount) will rightfully assume 5 is the number of orders being taken.
In other words… every time takeOrder function runs it will tell his best friend orderCount. But orderCount is also best friends with getSubTotal. So when getSubTotal calls orderCount on the phone orderCount will tell getSubTotal how many times takeOrder ran today.
That’s how getSubTotal knows takeOrder’s secret.