When adding variables inside a function


#1

In the first screenshot , the function getSubTotal uses a parameter “itemCount” and then when the same function is called later, it takes “orderCount” as an argument, which is a variable defined on the top of the code as well as used with a math method ++; inside the function takeOrder
This is the approach this codecademy lesson example uses.

I became curious if I could just use “orderCount” as the parameter for the function getSubTotal instead of “itemCount” because I thought maybe I don’t need to create one extra variable to point out the same thing. So I created a code in the second screenshot, and it seems to yield the same output. - Apparently as a new user, i can’t add more than one image, so here is the alternative code:

let orderCount = 0;

const takeOrder = (topping) => {
orderCount ++;
return Order: pizza topped with ${topping}
};

const getSubTotal = (orderCount) => {
return orderCount * 10
};

console.log(takeOrder(‘mushroom’));
console.log(takeOrder(‘pepparoni’));
console.log(Total Quantity: ${orderCount});
console.log(Subtotal: $${getSubTotal(orderCount)});

Could you tell me which code structure (between code in the screenshot and the alternative code I wrote) you like better and why? And is there any error or not-so-good element in the alternative code? I’d love to learn how the real programmers really thinks because i am new to coding. Thanks!


#2

Your function shouldn’t be accessing things that are outside itself, if you for example later call your function with the value 5, your 5 would get ignored because your function doesn’t care what the argument given to it was, it looks at itemCount instead

function add(a, b) {
  // this function ignores arguments given to it,
  // so why bother having parameters?
  // (a and b are not used)
  return c + d;
}
let c = 7;
let d = 8;
console.log(add(1, 2));  // should print 3

#3

Thank you for the reply! I wish I can understand what you mean. I think some of it is beyond what I am learning now. Once I have more experience, I will come back.


#4

Nothing complicated there.

Functions take some input and produce some output.

What you’re suggesting is to ignore input. If you do that, the function stops being useful.


#5

This is opening my eyes, so I appreciate it. I tried to compare the code structure you wrote for me with mine to understand which indicates what. Still struggling a bit. Would you expound a bit more on "What you’re suggesting is to ignore input.’? I think I get it about 50%.


#6

I am new here too, but I think it really doesn’t matter what you name the functions parameter since its just a ‘place holder’ for a value, of course it’s best to name the parameter that relates to the programs function.


#7

I have a question on the exact same lesson - orderCount vs itemCount.
I understand why to give them different names, but I don’t understand how the program knows how many items to multiply by 7.5

does ‘orderCount++’ tell itemCount how many pizzas were ordered?


#8

The program knows how many items are multiplied by the function ‘takeOrder’.

orderCount++ is the way how the function calculates.

Everytime the takeOrder function is called it will use ‘orderCount++’ to add 1 to the current value of the global variable orderCount.

You can try this to understand it a little more:

call takeOrder as many times you want and simply ‘console.log(orderCount);’ to see how many orders has been taken.


#9

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