Step 2 of Return Functions Question


#1

I was working on adding in orderCount in step 2 of the ‘Return’ section so that each time an order is placed, it adds +1 to the order. At first, while following the steps of adding in a new function, I assumed it needed to be placed at the bottom of the code inside of the function takeOrder as that’s where I was told to add it in. So my code looked like this:

let orderCount = 0;

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

takeOrder('mushroom', 'thin crust', orderCount++);
takeOrder('spinach', 'whole wheat' orderCount++);
takeOrder('pepperoni', 'brooklyn style', orderCount++);


After trying a number of ways to make the orderCount add 1 for each order placed, I eventually gave in and got the code from the program to find it looked like this:

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');

This threw me off because it looks like orderCount++ was placed inside the block rather than the function. My question is, while the step asked to add in orderCount to the function takeOrder, which is located outside of the block, why is orderCount added inside of the block?


#2

this is the takeOrder function:

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

so this is where the exercise told you to add orderCount++, the takeOrder function.

this:

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

are the function calls. The function call will execute the function, then the code in the function gets executed, which is where you want to increase the order count


#3

Oh okay, I see how that works now. PARAMETERS.
I was just thinking of the first few lines as a variable with a block of code rather than thinking about where parameters are set. If I am correct, orderCount is considered a parameter?


#4

your function has two parameters:

const takeOrder = (topping, crustType) 

topping and crustType, so when you call the function you need to supply two arguments to satisfy the parameters:

takeOrder('mushroom', 'thin crust');

nope, orderCount is a global variable, within the function you update the global variable


#5

I meant the orderCount++ within the block. But even so, I understand the differences. Thanks for the help!


#6

If you read my answer, you could deduce that you have two parameters: topping and crustType

so is orderCount a parameter? Nope, parameters are defined here:

const takeOrder = (topping, crustType) 

orderCount is not there, so its not a parameter.


#7

Right. What helped is realizing the difference between a function itself and function calls. I was placing orderCount++ in the wrong section. Knowing that, it seems a lot easier than I was making it.


#8

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