Isn't itemCount uneccessary?


#1

I’m not sure what itemCount serves if we already have the orderCount
can’t we just use:
const getSubTotal = () => {
return orderCount * 7.5;
};

Is there any advantage to declaring itemCount in the getSubTotal function and then having to asign the orderCount to it later?


#2

I have the same question: what is the difference between itemCount and orderCount? Are both variables/parameters necessary?

From this exercise:
https://www.codecademy.com/courses/learn-javascript-functions/lessons/functions/exercises/return?action=lesson_resume&course_redirect=introduction-to-javascript

Refer to the code below.

var 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;
};

console.log(getSubTotal(orderCount));


#3

Same question. Why add one more interchangeable variable to make things complicated?


#4

I got really confused over the use of orderCount and itemCount, itemCount works yes, but it just appears from nowhere, with no explanation.

I altered my code like so:

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 = (orderCount) => {
  return orderCount * 7.5;
};

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

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

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

Unless there is a distinct reason for itemCount, that was left out, I think that my code is syntactically correct, and it works perfectly.


#5

I am glad this was asked. itemCount completely threw me off this whole lesson, made the whole thing very confusing. I am more confused about how this even works with itemCount since we never assigned it a value?


#6

yes, thanks for mentioning. I’m also confused on this point, namely

-why not return orderCount * 7.5 as asked above
-why isn’t itemCount defined at top ? (why not say, let itemCount = 0);


#7

Yep, same confusion here…


#8

I as well, was thrown off from the minute they started using itemCount, I just assume it was supposed to be orderCount and they accidently typed the wrong parameter.


#9

Hi all,

Regarding that questions, I would like to share a few previous topics that have been asking about similar issue and have been answered several times. Hope you guys can refer some of them:

The post above explains @doraemonpumpkin’s question about why there is no need to define itemCount, because when orderCount (the argument) get called in the function, in this code:

console.log(getSubTotal(orderCount));

the value of orderCount is then passed as replacement of itemCount (which is the parameter) and the code inside the function will be interacting with that particular value pass to it.

And yes, @gigawhiz32951, you also can have orderCount as parameter (but it is not the best way to do it when you have thousand lines of code mixing parameter and argument with global variable)

In the end parameter (itemCount) serves as a placeholder only to be passed in a real value when the function is called. In this case, orderCount holds the value. If anyone is curious about other variation of questions and explanations, I have cataloged a few of the similar topics under my post here:

About why itemCount needs to be the parameter? That’s just the author’s choice I think. When you learn further, global variable is not always a good thing to have. Maybe the lesson just intended for the learners to learn about the variation of coding parameter, passing argument with function calling, as itemCount the parameter is passed with orderCount a variable which hold numerical value, while topping and crustType parameters to be passed with string values as arguments during function calling.

Happy coding! :slight_smile:


#10

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