itemCount value?


#1


https://www.codecademy.com/courses/learn-javascript/lessons/functions/exercises/return-ii?action=lesson_resume&link_content_target=interstitial_lesson

I'm not having issues completing the work but I am looking for some clarification.

Looking into it further I failed to see where any value was being declared for itemCount, but the code still worked. I changed the parameter to take the variable orderCount, which actually made sense because orderCount has a value, it works the same. What's up with that?

Maybe I'm thinking about it too hard...


var orderCount = 0;

function takeOrder(topping, crust) {
    console.log('Pizza with' + crust + ' crust' + ' and'  + topping);
  
  orderCount = orderCount + 1;
}

function getTotal(itemCount) {
  return itemCount * 7.5;
}

function getTax() {
  return getTotal(orderCount) * .06;
}
 
function total() {
  return getTax() + getTotal(orderCount); 
}


takeOrder(' chicken', ' thick');
takeOrder(' pepperoni', ' cardboard')
console.log ('Place order for ' +orderCount);
console.log ('the total is ' + total());


#2

If orderCount is defined outside of a function, you shouldn't need to include it as a parameter. It will work the same, but it's unnecessary.


#3

Right but that doesn't explain where the value for "itemCount" comes from.


#4

var orderCount = 0;

function takeOrder(topping, crustType) {
console.log('Order:' + crustType + ' pizza topped with ' + topping);
orderCount = orderCount + 1 ;
}
function getSubTotal (orderCount){
return orderCount * 7.5;
}
function getTax () {
return getSubTotal(orderCount * 0.06);
}

takeOrder('bacon', 'thin crust');
takeOrder('Sausage', 'thick crust');
takeOrder('onions','deep dish');
console.log(getSubTotal(orderCount));
console.log(getTax(orderCount));

https://www.codecademy.com/courses/learn-javascript/lessons/functions/exercises/return-ii
I am getting the tax but for some reason it won't let me past #2


#5

I'm glad others have noticed this, is anyone any the wiser? I was scratching my head wondering where it got that the itemCount was 3, so like others have done, changed it to orderCount and it worked perfectly fine. :weary:


#6

The odd thing to me was that it worked the same with itemCount and
orderCount, when orderCount was the only one with a value being defined.


#7

Hi @ben.grzybowski, @odie8391,

itemCount has no numerical value, it's just serve as its purpose as a parameter in a function.

For constructing function getTotal(), you'd want the function to take in "something" and return it by multiplying that "something" with 7.5. That "something" is called parameter. When calling the function, you'd want to pass argument into the function in replace of parameter.

Basically, you can put any name as a parameter, not necessarily named itemCount.
However, for this exercise, it's is best to follow the instruction from the author for the learning purpose and to pass the exercise with the validation system behind.

Outside of this exercise, you can write your parameter any way you want, such as:

function getTotal(unknownNumber) {
  return unknownNumber * 7.5;
}

or

function getTotal(funStuff) {
  return funStuff * 7.5;
}

but, it is best practise to have named your parameter with something more relevant, maybe like this: (if you're quite confused with the name of itemCount)

function getTotal(quantity) {
  return quantity * 7.5;
}

The code will work, as long as you're calling the function with getTotal(orderCount), because orderCount is a defined global variable with value, (with 0 assigned to it). If you try calling the function getTotal(itemCount), you will received ReferenceError: itemCount is not defined. In this case, itemCount is not a global variable, it is the parameter for the function, so it is locally defined and scoped to the function. Therefore, if you call getTotal(itemCount), the error throws as itemCount cannot be accessed.

I guess most confusion circle around inserting parameter vs calling a function with argument, see my comment below:

function getTotal(itemCount) {  //<==== this itemCount is a parameter, 
  return itemCount * 7.5;
}

function getTax() {
  return getTotal(orderCount) * .06; // <==== this orderCount is not a parameter, this is calling function getTotal with orderCount (the variable) as argument
}
 
function total() {
  return getTax() + getTotal(orderCount);  //<==== this orderCount is not a parameter, this is calling function getTotal with orderCount (the variable) as argument
}

Like @825orion said, using orderCount as a parameter will work too. However, it is best avoid using orderCount or any same Name as a parameter when it exists as a variable itself is to reduce confusion or accidental error.

Cheers. :slight_smile:


For more examples read my reply here with links to other topic as well:


How does "itemCount" fit in the pizza exercice when learnig java script
#8

Hi Ben!

I had the exact same problem as well. After looking at the code and reading Codexthon's comment this is what i I think is happening:

When the computer reads the code, function getSubTotal(itemCount) function isn't activated until it gets down to

function getTotal () {
return getSubTotal(orderCount) + getTax();
}

The computer reads and calls the getSubTotal function and orderCount becomes the value of itemCount. itemCount is just a placeholder for any value that can be put in getSubTotal()

That being said I'm still figuring out why its written this way - seems a bit confusing still to me. Hope this helps!


#9

Yeah what your saying does make some sense, I was having a hard time
wrapping my head around it...But I think I understand a little better now.
One thing I've learned since I've been writing code, is that, sometimes you
just have to accept that you don't understand why something works, and just
be happy because it works... Words of advice. Haha. Cheers.


#10

haha I agree, actually just went through some of my school notes and saw this - it kind of breaks it down in simpler terms.

To create a custom function you would use the following syntax:

function myFunction( a, b )
{
** return a * b;**
}

To use the function:
var x = myFunction(4, 3);

In this case values for a and b aren't explicitly defined, they are defined when the function is called upon and place between the brackets. In the codecademy exercise I imagine it like function inception. Instead of an actual value it calls on a variable that has a value.


#11

Oops it didnt bold properly it should really just look like this

function myFunction( a, b )
{
return a * b;
}


#12

I have the same problem understanding itemCount. Is that just like empty parameter and when you change it with orderCount it goes like this function
getSubTotal(orderCount) {
return orderCount * 7.5;
}
??

I did this exercise in more logical way to me. Can someone look at it and tell me what are the downsides of my version.
TY

var orderCount = 0;

function takeOrder(topping, crustType) {
console.log('Order: ’ + crustType + ’ crust topped with ’ + topping);
orderCount = orderCount + 1;
}

function getSubTotal(itemCount) { //Value of itemCount i dont understand. Why do i need to make getSubTotal as function?
return itemCount * 7.5;
}

function getTax() {
return getSubTotal(orderCount) * 0.06;
}

function getTotal() {
return getSubTotal(orderCount) + getTax();
}

takeOrder(‘bacon’, ‘thin’);
takeOrder(‘pepperoni’, ‘regular’);
takeOrder(‘pesto’, ‘thin’);

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

/* MY VERSION

var orderCount = 0;
var getSubTotal = 7.5;

function takeOrder(topping, crustType) {
console.log('Order: ’ + crustType + ’ crust topped with ’ + topping);
orderCount = orderCount + 1;
}

function getTax() {
return getSubTotal * orderCount * 0.06;
}

function getTotal() {
return getSubTotal * orderCount + getTax();
}

takeOrder(‘bacon’, ‘thin’);
takeOrder(‘pepperoni’, ‘regular’);
takeOrder(‘pesto’, ‘thin’);

console.log('Net= ’ + getSubTotal * orderCount);
console.log('Total= ’ + getTotal());
*/


#13

Veljko Ikonić - From my understanding, yes itemCount is simply just a place
holder, later the orderCount gets passed in it’s place.
If your version works, then great! That’s the thing with coding, there’s no
one set way to get from point a to point b, there’s a million different
ways to do it.


#14

I am having the same problem understanding this.

I read around in some java material and did come to an understanding that “itemCount” is just an arbitrary placeholder.

The problem I am having is, how does it know to count takeOrder and not some other list that may be below it? What if there were many lists on this page? The takeOrder list does not appear to be within any brackets or term. What connects the itemCount directly to takeOrder? Essentially, this function would count every item listed on the page, correct?

But it doesn’t, if you copy:

takeOrder(‘bacon’, ‘thin’);
takeOrder(‘pepperoni’, ‘regular’);
takeOrder(‘pesto’, ‘thin’);

after the console.log function it doesnt count it. Furthermore, if you change takeOrder to makeOrder, it has errors. So what makes this program know to look for “takeOrder”?

Would it know to use the takeOrder function if many other functions are on the screen?


#15

Hi everyone,
I still struggle to understand where itemCount gets its value from? To me it doesn’t seem connected to any rest of the code? In which part of the code does it say that itemCount receives the value of orderCount? Lost and hoping for some clarification :-S Thanks a lot,
Marion


#16

Hi @codexthon,
Can you please elaborate this paragraph on your explanation about local variables.I can’t understand this part of yours.

Please adjust with me as I am a begineer.

Regards


#17

Hi @mmangold, @hari123456789,

I try my best to answer this together for the last time. Please bear in mind, I’m not a JavaScript Jedi, but I do feel comfortable in understanding what parameter is to my own knowledge.

Note: It’s TL;DR post.


:new_moon: First Part: (the Unknown)


I’ll make another simple example:

Let’s say 1 orange cost you $5. I will ask you to buy an amount of oranges, but I will not tell you the quantity for now. How’d you create your formula to count the total cost of oranges?

Mathematical Approach:

1 orange = $5,
Total Cost:
X amount of oranges = X * $5 

English sentence logical-explanation approach:

One orange costs $5,
Total Cost should be the total of how many oranges you would buy multiply with $5.

Computer Programming Approach: (JavaScript)

var orange = 0;   // <=== When you still not buying any orange, you have 0 orange.
function totalCost (quantityOfFruit) {
      return quantityOfFruit * 5
}

Notice the similarity?

It doesn’t matter to what names we’d give to the Unknown amount of Oranges:

  • In Math, we called it: X
  • In English, we called it: the total of how many oranges you would buy
  • In Programming, we called it: quantityOfFruit or any names you want (more on this in Second Part)

:waxing_crescent_moon: Second Part: Programming Part (Understand what is Parameter vs Argument)


Part A: Parameter

Using my Orange Example:

Notice when I wrote my code, quantityOfFruit is inserted in between brackets ( )

var orange = 0;   // <=== When you still not buying any orange, you have 0 orange.
function totalCost (quantityOfFruit) {  
      return quantityOfFruit * 5
}

:cyclone: What goes into the bracket when writing a function is called “Parameter”, during this phase, you’re not calling or invoking function yet, you’re passing the item into the function, so it can use that particular item inside it’s code block.

Like I mentioned in my previous post, you can put any names as parameter, it doesn’t matter.

You can write it:

var orange = 0;   // <=== When you still not buying any orange, you have 0 orange.
function totalCost (unknownAmountOfOrange) {
      return unknownAmountOfOrange * 5
}

Or

var orange = 0;   // <=== When you still not buying any orange, you have 0 orange.
function totalCost (amountXOrange) {
      return amountXOrange * 5
}

Any name will do. But of course, you would want the names something related to your case.

Part B: Argument

So far, we have not try calling the function yet.

Now, I will tell you, the amount of orange I want you to buy is 10 oranges. Tell me the total cost for those oranges.

How would you write your code then?

var orange = 0;  // <=== When you still not buying any orange, you have 0 orange.

function totalCost (quantityOfFruit) {  
      return quantityOfFruit * 5
}

orange = 10;     // <=== update the amount of orange into 10 oranges
totalCost (orange);   // <=== call the function to determine the Total Cost.

Take note on:

totalCost (orange);  // <=== We invoke or call the function by passing in Argument

:cyclone: Notice orange is being passed into the totalCost function. That orange is referred as argument, the process we called, passing argument into the function.

To imagine how the computer interpret it, you can roughly imagine it this way:

orange = 10;     // <=== orange is now updated to 10
totalCost (10) { // <=== passing in the referred value
      return 10 * 5   // <=== equals to 50
}                // <=== return 50

Part C: Parameter and Argument using same name?

The another question here is, can I use orange as the parameter name?

var orange = 0,   // <=== When you still not buying any orange, you have 0 orange.
function totalCost (orange) {
      return orange * 5
}

orange = 10;
totalCost (orange);

Sure, you can. Like I said, you can put whichever name you want.

:star2:This is where and when, people start answering: It still works! :slight_smile:

BUT,

Think of this scenario. When you have written thousands lines of code, would you want:

  • your variable names
  • parameter names
  • arguments names

all mixing up together or share the same names all over? :thinking:

It is not wrong to share the same name, it’s just something the best practices trying to avoid.

Part D: Using Generic names for Parameter (Benefit)

So now, if I tell you to solve more question:

Buy 5 apples, costs $5 each.
Buy 2 bananas, cost $5 each.

You can straight away use the same code again because the generic parameter name quantityOfFruit

var orange = 0,   // <=== When you still not buying any orange, you have 0 orange.
function totalCost (quantityOfFruit) {
      return quantityOfFruit * 5
}

orange = 10;
totalCost (orange);

var apple = 5;
totalCost (apple);

var banana = 2;
totalCost (banana);

:star2: In this case, you will see and understand why using quantityOfFruit as the parameter name is better versus using orange as the parameter name. It makes the code more relatable.


:first_quarter_moon: Third Part: Scope (Global vs Local)


What is global variable, @hari123456789

In the orange case,

var orange = 0,   // <=== This is global variable
function totalCost (quantityOfFruit) {
      return quantityOfFruit * 5  // <=== This is ==NOT== global 
}

orange = 10; // <=== This is global variable
totalCost (orange);

var apple = 5;  // <=== This is global variable
totalCost (apple);

var banana = 2;  // <=== This is global variable
totalCost (banana);

When I said in my previous post, parameter is local, not global variable:

You can try console.log(quantityOfFruit), you will get quantityOfFruit is not defined, because it is not declared any where in a global scale.

Note my explanation on parameter: What goes into the bracket when writing a function is called “Parameter”, during this phase, you’re not calling or invoking function yet, you’re passing the item into the function, so it can use that particular item inside it’s code block.

So in this case, quantityOfFruit is scoped inside the function, for the function to use it. Therefore, it’s local. To simplify it, the function is using quantityOfFruit for it’s private use. You cannot accessed it anywhere outside the function, unless you pass something into the function.


:waxing_gibbous_moon: Fourth Part: What names you called Parameter?

(Placeholders? Empty holders? Replaced value? PassbyValue? Arbitrary names? … etc)


To me, I just need to understand what thing is considered as parameter. As long as you get how parameter works, it doesn’t matter what names used to describe it.

I found a good read on this user’s answers, asking about what is parameter:

Some may say parameter is a placeholder, others may agree to disagree. But, back to our case here, it doesn’t change the fact that

function getTotal(itemCount)  //<=== itemCount is the Parameter

later on...

getTotal(orderCount) //<=== orderCount is the Argument


:full_moon: Fifth and Final Part: Discussion on Writing Codes


There are many ways to write a code, just like languages, we learn vocabulary and grammar, but everyone writes and speaks differently, it also depends on what you want to achieve (the message relayed or the final results).

Just like what @ben.grzybowski said,

I guess if the code is written this way, most of the question about itemCount would not surface.

var orderCount = 0;

function takeOrder(topping, crust) {
    console.log('Pizza with ' + crust + ' crust and '  + topping);
    orderCount = orderCount + 1;
}

function getTotal() {
  return orderCount * 7.5;
}

function getTax() {
  return getTotal() * 0.06;
}
 
function total() {
  return getTax() + getTotal(); 
}


takeOrder('chicken', 'thick');
takeOrder('pepperoni', 'cardboard');
console.log ('Place order for ' + orderCount);
console.log ('the total is ' + total());

To me, when learning to code, it is equally important to try understand the code others have written. If you want to work as developer, you’re bound to receive/debug/view codes from others. It is not our position to say which one is better, because in this case, the code provided in this exercise works perfectly.

@betawhiz53102’s code works too:

var orderCount = 0;
var getSubTotal = 7.5;

function takeOrder(topping, crustType) {
console.log('Order: ' + crustType + ' crust topped with ' + topping);
orderCount = orderCount + 1;
}

function getTax() {
return getSubTotal * orderCount * 0.06;
}

function getTotal() {
return getSubTotal * orderCount + getTax();
}

takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');

console.log('Net= ' + getSubTotal * orderCount);
console.log('Total= ' + getTotal());

Just that you have to consider what difference does it make?

By having var getSubTotal = 7.5; as a global variable, anyone can make changes to the price, such as changing it to 0.01.

In the end, when the code written differently, it will have different impact, sometimes it’s just different style; other times, it has different approach or functionality.


Conclusion


If you have made it this far, I’d say thank you for spending your time in reading this post. When I said I’d try my best, this is the best that I can do. Any further clarification needed, it is totally out of my reach.

I really hope this post is error free and it helps explain the situation better.

I’ve spent hours typing this. :stopwatch:

I guess this will be the last itemCount topic I’d reply.
:sneezing_face: :face_with_thermometer: :mask:


:new_moon_with_face: Note: Extra

One last section I’d leave for anyone to go through:

The questions people usually ask are: (crafted similarly to my Orange Example)

  • I don’t see where quantityOfFruit being declared anywhere?
    -Short Answer: It is just a parameter, need not to declare globally.

  • What is the value of quantityOfFruit? 0? 1 ?
    -Short Answer: the parameter represent the unknown amount, so there is no numerical value there

  • How quantityOfFruit related to var orange
    -Short Answer: one is parameter, the other one is global declared variable

  • Why quantityOfFruit is not used when function call.
    -Short Answer: you don’t invoke/call a function with parameter

  • quantityOfFruit is not even used in the end, why even use it in the code?
    -Short Answer: I don’t understand the question’s logic

  • I replace the orange inside quantityOfFruit, the code still work, why even use quantityOfFruit to begin with?
    -Short Answer: see above, Second Part, Part C: Parameter and Argument sharing same name


:full_moon_with_face: With this, I rest my case. Thank you. :syringe: :pill: :skull: :sleeping_bed:


How does "itemCount" fit in the pizza exercice when learnig java script
itemCount and orderCount
#18

Hi @codexthon,
I have no words to say .You made me understand clearly.
For this part of your post ,I would say thank you for spending your time in answering my post.

I am sure not to ask a question after this post.:sweat_smile::ok_hand::+1:
Regards


#19

@hari123456789,

Really glad if it helps. Makes my day if I can help just a little.

If you have other doubts, feel free to ask those questions.

Just that I’ve overspent my energy a bit.

Maybe other frequent users may be able to help answering your further questions. :wink:


#20

Hi @codexthon ,
In this case item count is temporary an vanishes when the function block ends and store its value to the getTotal() function and when in this part,

function getTax() {
  return getTotal(orderCount) * .06;
}
 
function total() {
  return getTax() + getTotal(orderCount); 
}

The getTotal() takes in the value which was previously stored and uses it.So,basically order count and item count are the same right?.
Pls correct me ,if something is wrong.

I need some clarification .I am sorry to trouble you .I tried understanding but only this part is causing troubles in my mind.I would be very much happy if you could clarify .I guess this will be the last post in this section.


One last time a big SORRY.


Regards:grinning::cold_sweat: