Having trouble understanding this simple exercise:



I’m just trying to understand this exercise… Only have 1,5 days of JavaScript under the belt.
From what I understand, var inputs a value.
Function doesn’t do anything by itself, but just loads itself in memory, and whatever’s written in the block will be executed, once the function is called upon.
What happens to orderCount? How does it count? I presume, once the function takeOrder is called upon, orderCount gets a ‘+1’ ticked to itself? If that’s the case, what is the specific reason the orderCount has to be placed in ‘function takeOrder’?

The ‘function getSubTotal(itemCount)’ totally freaks me out!
I understand that the 7.5 is the price per pizza; and that the itemCount depends on how many times the function right above it is triggered. (I do presume that after executing ‘function takeOrder’, it goes to execute ‘function getSubTotal’?

The takeOrder is just triggering function statements above;
and console log is printing the ordercount (which ticks up every time the takeOrder function is triggered), times the price.

What gets me confused is the getSubTotal(itemCount), with a return value… what happens to the ‘getSubTotal’ or ‘(itemCount)’ after the function each time the program is ran (or the function is triggered)?
Don’t get it :frowning:

var orderCount = 0;

function takeOrder(topping, crustType) 
{console.log ('Order:',crustType,'crust topped with',topping);
orderCount = orderCount + 1;}

function getSubTotal(itemCount)
{return itemCount*7.5;}




I too am confused by the “itemCount” function, it seems like the “itemCount” is undefined, or assumed even, to me. I don’t get where it gets the “itemCount” from. I know that it counts the “takeOrder” but what I don’t understand is how it knows to count “takeOrder” as an itemCount. Where does it translate “takeOrder” into “itemCount”?


i will try to explain this. i am not an instructor so forgive me if i don’t explain it properly.

the ‘itemcount’ is a parameter of the function. so when you call the function the part in parentheses is ‘itemcount’. for example if you call the function like this: getSubTotal(7); then 7 would be the itemCount. however, this example is using orderCount as the parameter (instead of 7 in my example). so whatever the orderCount is at the time the function is called, that is what the value of itemCount will be.

orderCount starts at 0 so the first time you call the function itemCount will also be 0. so to the program you are essentially calling the function like this: getSubTotal(0). however, the orderCount will increase by 1 every time the takeOrder function is called, which thereby increases the itemCount as well.

i hope this makes some sense? the orderCount and the itemCount will essentially be the same because we are using the value of orderCount as the value of itemCount.

also, calling the takeOrder function doesnt execute the getSubTotal function. you are calling the getSubTotal function using the console.log statement at the bottom. one doesnt automatically run when the other does. and the only relationship they share is that orderCount is being used as the function parameter in getSubTotal.

hope this makes sense. if not just reply with anything else you dont understand and ill do my best to try and explain it further.


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