# Need a breakdown on itemCount parameter, what is going on?

let orderCount = 0;

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

takeOrder(‘mushroom’, ‘thin crust’);
takeOrder(‘spinach’, ‘whole wheat’);
takeOrder(‘pepperoni’, ‘brooklyn style’);

const getSubTotal = (itemCount) => {
return itemCount * 7.5;
};

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

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

``````console.log('Order sub total is: \$' + getSubTotal(orderCount));
console.log('Order Total with taxes is: \$' + getTotal(orderCount));``````

For this exercise we are shown three ways to access `orderCount`.

`getTax()` takes no parameter, as I recall. It accesses `orderCount` directly from within the function.

We will need to check this to be sure. Please post a link to the exercise. Thanks.

It is lesson 6 from Functions, Return II

My question is regarding itemCount from functions, getSubTotal = (itemCount) , getTax = (itemCount) & getTotal = (itemCount)

Okay, seems my memory of the course is different from what it is now. Must have been edited since I did the unit last year. The parameter is indeed `itemCount` for all three functions.

`itemCount` does not have a value outside of any function. That is why we have to pass in `orderCount`. For this exercise I’m not sure we have to log both subtotal and total, but at any length, we should only log the amount, and no other text.

So, can you correct me if I am wrong. What I understand is that basically the first time the function getSubTotal = (itemCount) is declared, the itemCount is kind of like a dummy(placeholder), the real value that is important here is the multiplication by 7.5? Which in turn is later used to complete the following operations from the functions getTax and getTotal, which in turn is later used to call the functions in the console.log’s with the orderCount argument?

Once we make this call,

``````console.log(getTotal(orderCount));
``````

`orderCount` is in play, but as taken in as `itemCount` and that value passed to the `getTax` and `getSubTotal` functions.

``````const getTotal = itemCount => {
return getSubTotal(itemCount) + getTax(itemCount);
};
``````

Perhaps I am not clear on the earlier, shorter exercise. Could you tell me from the getSubTotal function, where did itemCount parameter come from and what is its purpose? Could you also put in words what is happening in the console.log(getSubTotal(orderCount)); ? Maybe this will make it more clear for me. Many thanks, its just that it is foggy and I cannot continue to the next lesson. Sincere Thanks!!

let orderCount = 0;

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

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

takeOrder(‘mushroom’, ‘thin crust’);
takeOrder(‘spinach’, ‘whole wheat’);
takeOrder(‘pepperoni’, ‘brooklyn style’);

console.log(getSubTotal(orderCount));

This is from Lesson 5 : Return

It’s purpose is to isolate the value in a local variable. Parameters are local and cannot be accessed outside of the function. They are like a placeholder for an expected (needed) value in the function.

``````const getSum = (a, b) => a + b;
c = getSum(6, 7);
console.log(a, b, c);
``````

ReferenceError: `a` is not defined

The name we give our local variable(s) is arbitrary. Once the function is exited, garbage collection takes place on all the local variables and they cease to exist.

1 Like

Could you put in words what is happening in the console.log(getSubTotal(orderCount)); ?

We are logging the return value of the call to `getSubTotal()` in which we pass the current value of `orderCount`. The value is multiplied by a constant, `7.5` and the computed amount returned to the caller where it is logged.

1 Like

Oh, Wow!! I printed and re-read everything you told and it all makes sense now!! It is crystal clear! Thank you very, very much !! Now it seems more obvious, of course not before. Deeply appreciate it, have a great day!

1 Like

I’ll leave you with a little cheatsheet to refer to until this is cemented in your mind (you won’t need it very long, though)…

``````function funcName(parameters) {
// comment
// code block
return someValue;
}
``````

The above is known as a function declaration. It is called so because there is no assignment. The function is declared, and hoisted to the top of its current scope. This function can be written anywhere in the source listing, even the very bottom but will still exist and be callable immediately at session runtime.

Functions can take functions as arguments, and can return a function. You’ll see this more once the subject of `callbacks` comes up, or when you reach the more advanced concepts.

The argument is what we send or pass into a function through a call expression.

``````funcName(arguments);
``````

A function can have from zero to `n` arguments, though it follows `n` will be small. We set the number of arguments that the function can identify by the written parameters.

``````function getSum(a, b) {
return a + b;
}
``````

The above has two identified parameters, which means the function is expecting two arguments.

``````console.log(getSum(6, 7));

// <- 13
``````

What this means is that only the two arguments are recognized. Were we to call using more than two, they would be ignored…

``````console.log(getSum(6,7,8,9))

// <- 13
``````

No errors are thrown, and the output is not affected. I say “ignored” but that is not quite accurate. Not referenced is more the point. The arguments do exist in memory in what is known as the arguments object.

``````function getSum() {
return Array.from(arguments).reduce(function (a, b) {
return a + b;
});
}
``````
``````console.log(getSum(6,7,8,9));

// <- 30
``````

Don’t worry if this went right over your head. Just know it is not smoke and mirrors. You’ll reach this level of understanding in due course. Notice that our function has no parameters yet we can pass it any number of comma separated values. That’s all I wish to demonstrate at this time.

Mind, we do have arrow function syntax so why not give it a whirl?

``````const sumEmUp = () => {
return Array.from(arguments).reduce((a, b) => a + b);
}
console.log(sumEmUp(1,2,3,4));
``````

ReferenceError: arguments is not defined

Arrow functions do not have an arguments object so we need to revert to the imperative function approach. Below is a function expression where an anonymous function is assigned to a variable.

``````const sumEmUp = function () {
return Array.from(arguments).reduce((a, b) => a + b);
}
``````
``````console.log(getSum(1,2,3,4,5,6,7,8,9));

// <- 45
``````

It’s because of the nature of functions that we need to distinguish between the values a function receives and recognizes or has access to and the values sent to it. Hence the terms, parameters at the receiving (callee) end, and arguments at the sending (caller) end.

Functions have another object that you will become familiar with once you get into objects, the `this` object. Arrow functions do not have this object, either. They are meant to be pure functions so have no interaction with states and data around them. Imperative functions have no holds barred. That is where you find `this` and `arguments`.

``````obj = {
prop: "someValue",
showProp() {
console.log(this.prop)
}
};
obj.showProp();

// <- someValue
``````

Above we write a function inside an object. This is known as a method. Methods belong to objects, where functions belong to the scope where they are written and are not bound to any program defined objects.

The method as written has a `this` object. `this` is also known as `scoping the execution context`. An object has it so it can identify itself in a function. When you get to that unit everything will be covered.

We cannot use arrow functions as methods because of their lack of context so you will never see,

``````obj = {
prop: "someValue",
showProp: () => {
console.log(this.prop);
}
};
obj.showProp();
``````

since it will not display anything.

Lots more is coming down the pike, so forge ahead and refer back to this topic if questions come up.

1 Like

Thanks for your extra help! I will surely keep the cheatsheat close and handy!

1 Like