itemCount value


so my big problem is with the fact that there is no explicit definition of the value for itemCount parameter in the getSubtotal() function, and yet multiplying this same value by 7.5 gives a value of 22. i divided that by the 7.5 and obtained a value of 3 which i am implying is the value of itemCount before it was multiplied. Now someway some how that value matches up with the number of times the takeOrder function is called and changes to 30 when i call it one more time… can some one explain to me the relationships?.

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>


Replace this line with your code.

<do not remove the three backticks above>

we do link orderCount and itemCount:

// function declaration with parameter itemCount
const getSubTotal = (itemCount) => {
return itemCount * 7.5;
// function call with argument orderCount

see the comments, i am going to use those terms i specified there.

when we call the function, we supply an argument (orderCount) the value of which gets passed to the function parameter (itemCount), that is how the “linking” works, except in your case the function call is inside another function, the above example is a simplified program of your program

function only get executed when called, if the function isn’t called, it isn’t executed. Because of this, function parameters can be placeholders until they get there value from argument at function call

if it helps, when you call the function, this happens under the hood:

itemCount = orderCount

but JS is handling this for you.

1 Like

I see…makes a lot more sense if its receiving that value from orderCount. but if thats the case, then why dont we just pass orderCount directly instead of setting up new parameters and passing it to them instead?

directly from where to where?

which new parameters? to them refers to the new parameters?

There are to many references to functions without being named in your question, to understand what you are asking

please include: function names and preferable also snippets of code where you have questions about. I can see how the code currently is, as long as you name the functions (so i know where to look)

you can also include how you would design it, then i can explain why this is or isn’t a good idea

1 Like

Ok i think i have got it all now. Basically, itemCOunt is initially set as a parameter in the getSubTotal function and then the orderCount is passed as the argument. cool.

but what is was thinking of in terms of passing it directly is that we could have

const getSubTotal = () => {
return orderCount * 7.5; //orderCount variable gets multiplied directly instead of being passed as an argument

as against having to pass parameters and arguments again…
Perhaps it was meant for the purposes of further demonstrating how parameters and arguments work? and if not just for that reason, why does it work better to pass it(orderCount) as an argument rather than directly multiplying the global variable(orderCount).

that as well, but the actual reason is more difficult to understand. Hard coding the global variable (orderCount) everywhere into the function, makes making changes more difficult. Always code defensive, always expect that you might need to change something. The code adapts much better to change when we use parameters.

okay, in a program this small, its not so problematic. But good to learn good habits, better early then late

1 Like

One of the key points being illustrated in this exercise, apart from those already mentioned, re: arguments and parameters, is scope.

orderCount => global
itemCount  => local

A global variable is any that is outside of the function that accesses it. This is said to be in outer scope. In this exercise the variable resides at the top of scope, namely in context of the window object.

Local variables, on the other hand are defined within the scope of their containing function (an object). When we pass a global variable to a function, but give the parameter a different name, the value of the global is copied to the local, and any change made within the function to that variable is not seen on the outside of the function, and no change takes place to the global (unless it is a reference object such as an array or object).

orderCount is deliberately left outside of any functions so that it can be a running accumulator. As we see, every time a pizza is ordered that value increases by one.


Thanks alot @stetim94 and @mtf for that last bit… ive got it all now thanks to you guys…cheers

1 Like

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