5. Return


#1

Hello All,

I am having trouble understand what is going on in these functions:

let orderCount = 0;

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

};

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

Specifically, I don’t understand how the parameter itemCount is finding the variable orderCount that is being added with plus 1. The function getSubTotal is somehow knowing that the count is increasing but I don’t see how these functions are tied together. Thanks for the help!


#2

at the moment its not, function parameters are placeholders until the function is called. Then the parameter gets value from argument at function call.

so tie everything together, add function call(s)


#3

That’s backwards. When calling the function the caller can supply any value they want. The function creates a variable for the received value.

A function typically doesn’t care about anything outside itself. A function that adds two numbers doesn’t care about the time of the day. It only pays attention to the provided values.


#4

Ok Thanks! So I added the function calls and this is the solution to the problem:

let orderCount = 0;

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

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

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

console.log(getSubTotal(orderCount));

I can see that every time the takeOrder function is called, it adds one more to the orderCount. But I get lost within the console.log. I see that the getSubTotal multiplies the by the number of orders but why does this work? Why does the getSubTotal function know to put a 3 for the itemCount when this 3 comes from the orderCount?


#5

the argument at function call (orderCount) passes its value to the function parameter (itemCount)


#6

I hope my comments help to illustrate what is going on here.

let orderCount = 0; // orderCount is available **globally**, and is set to 0.

// takeOrder(topping, crustType) is called some number of times below. At present **THREE** times. 
// orderCount is increased in value by 1 each time takeOrder(topping, crustType) is called.

const takeOrder = (topping, crustType) => { 
orderCount++;   // **HERE**, the global variable orderCount gets bumped
console.log('Order: ' + crustType + ' pizza topped with ' + topping);
};

/*
* getSubtotal(), when called, will take that **global** orderCount (whatever value it has been increased to),
*  multiply orderCount by 7.5, and then return that value to the caller.
*/ 

// The parameter name "itemCount" is distracting. Just keep in mind that the number of orders and the 
// number of items might not be the same in the future..
const getSubTotal = (itemCount) => {   
return itemCount * 7.5;
};

takeOrder(‘mushroom’, ‘thin crust’);           // orderCount = orderCount + 1 ( so now orderCount = 1 )
takeOrder(‘spinach’, ‘whole wheat’);          // orderCount = orderCount + 1 ( so now orderCount = 2 )
takeOrder(‘pepperoni’, ‘brooklyn style’);  // orderCount = orderCount + 1 ( so now orderCount = 3 )

// We send the **globally available** orderCount to getSubTotal() and (orderCount * 7.5) is returned.
// console.log() sends that returned value to the console for display.
console.log(getSubTotal(orderCount));     

#7

Hi there, so here is how getSubTotal() knows the number of Pizzas ordered being 3:

First of all, don’t get confused by the name of the parameter within

the name “ItemCount” is just so you, the developer understand what it stands for. For all you care you could write something like

or

Now how does console.log(getSubtTotal(orderCount)); know the number 3?

The magic happens within your takeOrder function

it says orderCount++; which is the lazy way of writting orderCount = orderCount + 1;

The code above would work just the same and might be a little more descriptive about whats going on.

So every time you take an order (add a Pizza) and run the takeOrder function it changes the let orderCount = 0; by adding +1.

So the first time it runs it changes let orderCount = 0; to let orderCount = 1;
The second time it runs it changes let orderCount = 1; to let orderCount = 2;
The 3rd time it changes let orderCount = 2; to let orderCount = 3;

So because you have 3 Pizzas takeOrder() will run 3 times. Every time takeOrder() runs it will add +1 to the orderCount.

By the time the program is done taking Pizza orders and wants to get the sub total: console.log(getSubTotal(orderCount)); let orderCount no longer = 0; but let orderCount = 3;

So getSubTotal(orderCount)); uses the 3 as the parameter.

If the takeOrder function would run 5 times it would add +1 five times to let orderCount = 0; and 5+0=5 meaning getSubTotal(orderCount) will rightfully assume 5 is the number of orders being taken.

In other words… every time takeOrder function runs it will tell his best friend orderCount. But orderCount is also best friends with getSubTotal. So when getSubTotal calls orderCount on the phone orderCount will tell getSubTotal how many times takeOrder ran today.
That’s how getSubTotal knows takeOrder’s secret.


#8

I struggled at 1st too not realizing that it really did care about the name of the parameter .


#9

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