Questions on Functions Less 6 Return II (Clarification!)

Url for the exercise:

Functions: Lesson 6 Return II (getTax)

Below is part of the correct code, but I’m so confused as to why it is right. I ended up using all the Hints, which I hate doing. I can’t find why you declare itemCount and orderCount? Why can’t you reuse orderCount? I tried and it won’t do it.

let orderCount = 0;

const takeOrder = (topping, crustType) => {
  console.log("Order:" + " " + crustType + " " + "pizza topped with" + " " + topping);
  orderCount = orderCount + 1; //this makes sense!

const getSubTotal = (itemCount) => {  
//*why do you use itemCount? why does orderCount not work?*
  return itemCount * 7.5;

const getTax = () => {
  return getSubTotal(orderCount) * 0.06;
//*why do you return to use orderCount and not itemCount*

I just did this lesson and kind of wondered the same thing. I believe it is because orderCount is a parameter of getSubTotal so you can’t call on getSubTotal without also including the orderCount parameter. Not 100% sure if that is the correct interpretation, but that is how it made sense to me.

Global variables are visible and accessible to all functions, no matter where they are scoped. That’s what global scope refers to. In techical (objective) terms, they are properties of the window object.

 console.log(orderCount === window.orderCount);    //->  true

In the above program it serves as a permanent running counter. Any function can modify the variable, not just the takeOrder function. This means another function can roll back or reset the accumulated value. For our purposes we needn’t go there, but it is useful to know.

The parameter variable of a function is locally declared, and cannot be seen on the outside when it takes a value. If it takes a reference, then it is a local name for the reference (arrays, objects, functions). Changes made to the object by the function can be seen immediately in the global reference object. Not pertinent to this exercise, but again, useful to know.

In this program, orderCount is a value so gets copied to the parameter. This makes the function re-usable. Another part of the program may need a price look-up feature that does not depend upon the takeOrder function (or the orderCount variable). It will be sending arbitrary values to the getSubTotal function and using it as a handy computer.

const getSubTotal = itemCount => itemCount * 7.5;

The above is a valid form of your function from above. In technical speak, it is known as a pure function. Why? Because it always gives the same result when given the same input; and, it does not alter or interact with objects on the outside. This is the ultimate in re-usabile and portable code that any program can have plugged into it.

Now that JavaScript supports type="module" we have a place to keep all our re-usable pure functions, much like the Math and Date objects, That makes them available to any program.

The other two functions in this program are not pure. Can you figure out why, given the above explanation? Hint: orderCount is referenced from both of them. The ‘Why?’ in your question is, because the author wanted us to see how a parameterless function can see global variables.

The getTax function is dependent upon orderCount so is not portable or re-usable. Other functions cannot call it with arbitrary values like they can, getSubTotal. They would need special wiring…

const getTrialTax = trial => {
  let temp = orderCount;      // make a copy of the global
  orderCount = trial;         // set the global value 
  let trialTax = getTax();    // cache the return of the dedicated function

  // orderCount might have been changed by another function while
  // this trial value was in use so account for the change

  temp += orderCount !== trial ? orderCount - trial : 0;

  orderCount = temp;          // restore the original value
  return trialTax;
1 Like

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