Difference between itemCount and orderCount attributes?


#1


https://www.codecademy.com/en/courses/learn-javascript/lessons/functions/exercises/return-ii


basically i'm a bit confused between the itemCount and orderType attributes because to me they seem the same? when i replace the
function getSubTotal(itemCount) { return itemCount *7.5;}
with
function getSubTotal(orderCount) { return orderCount * 7.5;}
i get the same output values and no error messages.

i just don't really understand why the itemCount is needed in the first place, especially when the subTotal function is run the attribute is replaced with orderCount anyway.

hopefully this makes sense, i'm probably being kinda dumb not seeing this lol.


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

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

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

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

takeOrder('bacon', 'thin crust');
takeOrder('ham', 'thick crust');
takeOrder('pepper', 'stuffed crust');
console.log(getSubTotal(orderCount));
console.log(getTotal());


Functions 5/7 Going From orderCount to itemCount Help
The Answer to the function
Scope, variables, functions
#2

itemCount is a local variable of getSubTotal function.
It only exist when function gets called!

orderCount is a global variable !
Which gets updated whenever we call takeOrder function.

in other words itemCount is a parameter.

and orderCount is put here as Argument(value).
hope it helps!
do reply if it solves your confusion or not! :slight_smile:


#3

ahh okay, yeah it does thanks! :smiley:


#4

You're welcome! :slight_smile:
keep up the good work! :thumbsup:


#5

Thanks for the quick reply Greentreemee! I'll admit I'm still a bit confused (I learn by doing) but it did clarify a bit when you mentioned Item Count is a parameter and Order Count is the value. I'm still with yoolivia on why itemCount is needed in the first place since orderCount was already there....I think I just need to pass this concept into another scenario and see how JavaScript behaves then.


#6

itemCount is a variable that resides inside the getSubTotal function.

Why it is relevant to function but not other portion of code?
Variable inside functions are come into play when you call the function and pass arguments (values).
Everything get vanished when function call get finished.

Lets jump to orderCount, what is it and why it is relevant?

We're using orderCount as counter here, which gets updated when we call takeOrder
also It's a global variable.
Now ,when you ordered 3 times?

takeOrder('bacon', 'thin crust');
takeOrder('ham', 'thick crust');
takeOrder('pepper', 'stuffed crust');

because its get updated every time when you call takeOrder.

(Notice we did not used var before orderCount inside the takeOrder function, It could made it local variable.)

Then value of orderCount gets 3 so orderCount is a variable holding value 3.
Now we have to find what is total price of these 3 orders?
We have function to do this thing for us.
getSubTotal(itemCount)

then we pass orderCount in it as a value (which is 3).
getSubTotal(orderCount)

At last as we have to add tax, We call getTotal() .

I hope it helps! :slight_smile:


#7

ok now, that helped me a lot!...and raised another question lol...I appreciate all your help!

So my next question is: how does takeOrder signal orderCount to increase its number of orders?...is it just counting how many times we called the takeOrder function ?


#8

@rubyjumper25284

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

lets focus on that..
we set orderCount = 0. (note its a global variable and global variables can be modified by the functions)
How it is global? Defined outside!

When we call..
takeOrder('bacon', 'thin crust');

then takeOrder's runs its inside code...
As you can see at
,
orderCount = orderCount + 1;

runs and increase the value by one, Now orderCount have value = 1

Then you again call takeOrder function...
takeOrder('ham', 'thick crust');
,
orderCount = orderCount + 1;

runs and increase the value by one, Now orderCount have value = 2

Then you again call takeOrder function...
takeOrder('pepper', 'stuffed crust');
,
orderCount = orderCount + 1;

runs and increase the value by one, Now orderCount have value = 3

So You can clearly see the Global variable orderCount being changed by function i.e everytime the function get called it increases its value by 1.


#9

Hi, i am having the same issue as yoolivia. After reading ur answer, i still do not understand why is there a need for

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

in addition, idk if it sounds weird but itemCount dosent appear anywhere else in the remaining code so how does it contributing to calculate getSubTotal and where do you get the value of itemCount to be subbed into (return itemCount * 7.5;).

much thanks for any replies:)


#10

@potatoman50
Lets look at why we use functions.
Functions are like reusable ideas , like a Swiss Army knife that can get fits for many problems of same nature

Lets understand what I meant by that...

lets see we made a small function that takes a number and checks if number is zero or non zero.

function checkZero(x) {
    if (x == 0){
        return "Number is zero";
     }
    return "Number is not  zero";
}

Now why I called function a Swiss Army knife?
Because this special function can work for any number..

checkZero(9);  // It will return  "Number is not  zero"
checkZero(0);  // It will return  "Number is  zero"
checkZero(19);  // It will return  "Number is not  zero"
checkZero(12 % 3);  // It will return  "Number is  zero"

So your function can be used for many numbers!
so..
1. Function can solve so many problems of same nature
2. Function makes your code more well structured
3. It lessens programming bugs
4. neat and clean code.
5. also read this.. DRY principle

Now look at this code of yours

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

It could be written as

function getSubTotal(x) {
    return x * 7.5;
}

It could be again defined like this

function getSubTotal(potatoman50) {
     return potatoman50 * 7.5;
}

or

function getSubTotal(greentreemee) {
    return greentreemee * 7.5;
}

so the variables of function only matters to function and It can be any name.
It has no meaning outside of function.

when we pass something like this...
getSubTotal(3);

The itemCount or x or potatoman50 or greentreemee just takes value of 3 and
inside the body of functions gets multiplied by 7.5 and returned by function.

function getSubTotal(whatever name you put takes value of 3) {
    return 3 * 7.5;
}
// it returns  22.5

if you put 5...then

function getSubTotal(whatever name you put takes value of 5) {
    return 5 * 7.5;
}
// it returns  37.5

do reply if it makes sense or not :slight_smile:


#11

For the most part I thank you as your reply cleared my misunderstanding of naming the local variable which has narrowed down my question to, how does the value 3 get passed into the local variable of x or itemOrder or Potatoman50 or greentreeme??


#12

@potatoman50

how does the value 3 get passed into the local variable of x or itemOrder or Potatoman50 or greentreeme?

define a function

Lets define a function called fillCup

function fillCup(cup){
    # do something with value of cup
}

The variable cup is a parameter which have no value inside it...
Its empty and have no meaning.... (think it like an empty cup)

Calling

But lets say when we call this function by passing the value...

fillCup(3);


The parameter cup gets replaced by value 3.. (think it like cup getting filled by coffee)

so wherever (inside the function) it(parameter) is used ,that parameter is just replaced by the value (argument).

ending

When function call gets finished , every value that cup had, gets vanished
and it again gets its empty form(no value, dynamic form).

Does it make sense ?
Do reply! :slight_smile:
Update**
@potatoman50
I apologise for mixing it with some other language's syntax! (Its fixed! edited now)


#13

Hmm i hope you wont rage BUT i still don't get it, i understand the concept but applying it still dosent make much sense to me, below i will put another sample code. The code in the box are the emphasis, the rest is just reference.

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


function getSubTotal(itemCount) {
return itemCount*7.5; 
  #This outputs 22.5
}
/* so how did value 3 get into the bolded code to achieve 3*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());

#14

Its okay hehe :slight_smile:
What part of code does not make sense to you?


#15

ok the part where how is 3 being pass down to code at the bottom as i do not see any common codes(not sure with the terminology of similar in coding) that allow the value of 3 to be passed down into the code that allows the multiplication to take place

function getSubTotal(itemCount) {
return itemCount*7.5; 
  #This outputs 22.5
}

Help with 3/7 case
#16

function getSubTotal(itemCount) {
     return itemCount*7.5; 
  // This outputs 22.5 when itemCount is passes as value of 3 (or think as itemCount holds value of 3)
}

when you pass 3 as itemCount...

getSubTotal(3);
It means itemCount is being replaced by 3.
Now function looks for where itemCount variable is used(everything happening inside the function.)

Then it sees Aha there...

itemCount*7.5 -----------------> 3 * 7.5
Then function sees it has to return the value...
then It returns 22.5.

Update**
I have found few articles for you.
Javascript always passes by value. However, if you pass an object to a function, the "value" is really a reference to that object

passing by reference vs. passing by value

A small video explaining this concept--
JavaScript - Pass by value

Just give a read to them ,If it make sense to you its okay otherwise You can ask about what you not 'getting'. :slight_smile:


#17

Ok thx man, i sorta grasp it now as i am repeatedly reading through your replies and some other references, thank you for being ever so patient with me. If i have any other doubts is it plausible for me to ask you?


#18

Sure! :slight_smile:


#19

thank you so much u have been a great help! :slight_smile:


#20

Youre welcome! :thumbsup: