Return II - questions

I finished the Functions - Return II section, but I just can’t wrap my head around something. I simply don’t understand the role of the orderCount parameters in the different functions and function blocks.

I understand that you count the amount of orders in the takeOrder function. But after that, I just can’t wrap my head around how and why it’s used in the other functions? I’ve been going through the final code for 20 minutes, and I’m sitting here for 10 minutes trying to explain what I don’t understand, but I can’t seemt to articulate a specific question. I just don’t understand how and why this orderCount is manipulated in the later stages of the code…

Anyone has any enlightening words for me? :slight_smile:

let orderCount = 0;

const takeOrder = (topping, crustType) => {
  console.log(`Order: ${crustType} pizza topped with ${topping}`);
  orderCount += 1;

let getSubTotal = (itemCount) => {
  return itemCount * 7.5;

const getTax = (orderCount) => {
  return getSubTotal(orderCount) * 0.06;

const getTotal = () => {
 return getTax(orderCount) + getSubTotal(orderCount); 

takeOrder('mushrooms', 'thin crust');
takeOrder('tuna', 'cheese crust');
takeOrder('salami', 'american crust');

The exercise shows us the difference between global scope and local scope.

orderCount is global, itemCount is local. All functions can see a global but nothing outside of a function can see the local variables (including the parameter).

1 Like

Thanks, I think I’m starting to understand, also by reading the discussion over here: Syntax on Learn JavaScript:4. functions page 6/10

Only thing I’m still confused about is why the getTax function needs a parameter?

const getTax = (orderCount) => {
  return getSubTotal(orderCount) * 0.06;

Why not create the function without a parameter and simply return the tax value?

const getTax = () => {
  return getSubTotal(orderCount) * 0.06;
1 Like

Will need a link to the lesson to check this. Should that parameter be itemCount? Remember this is a demonstration, only.

Hi MTF, this is the link to the lesson:

Okay, I see you do have the right parameter. You are correct in saying that the parameter is not needed, but it goes back to the demonstration.

Something to note about the above. orderCount is a local variable, not the global, even though their names are the same. The parameter is said to shadow the global, which value is copied into the local variable. It would not matter what name we give it, a shadowed global cannot be seen by the function, only its local value.

This is not very well demonstrated, but it is something to look into further.

  • values are copied to local variables through the parameters.
  • reference objects (arrays, objects) are not copied, only their location is passed in.
1 Like

Thanks a lot for the explanation and clarification!

1 Like

I arrived to this discussion because I was having the same feeling than you. I think that the thing that was confusing me is that the parameter in getTax() was the same than the Variable used for the number of orders. I think it would be better to use a different name and that would help to not confuse the parameter with the actual variable.


I agree with David. It took me many days to finally understand that the parameters in a function are (as far as I know at this moment) only used to sent specific arguments to the function and to be able to manipulate the arguments within the function. What confused me, both here and at Treehouse, is the practice to use the same names for the variables and for the parameters. I think this could’ve been easily prevented by making it more clear that these are two different things by using different names.

The comment thread was helpful in making me understand local and global scope. But why getTotal() is not needing a parameter while the two function written in return inside it return getSubTotal(orderCount) + getTax(orderCount); require orderCount as argument.

In Go and some other languages, I have seen that its necessary to pass argument to the parent function if we want to pass it to functions inside it. Or I am missing something?

In some languages it is possible that global variables are not accessible from within a function, but in JS that is not the case. Global variables are accessible from anywhere in the program. They are properties of the window object.

The idea here is not to demonstrate best practice, but to see the various ways that variables are affected by scope. Take some time to do a little more reading on the subject of scope and function parameters.

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