Function and return- itemCount, orderCount and takeOrder relation


#1

Hello,
here i need something to be clear about the relation of orderCount and takeOrder.

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

in link above i found below code:

//
let orderCount= 0;
const takeOrder = (topping, crustType) => {
orderCount++;
console.log('Order: ’ + crustType + ’ pizza topped with ’ + topping);
};
const getSubTotal=(itemCount)=> {
return itemCount*7.5;
};
takeOrder(‘mushroom’, ‘thin crust’);
takeOrder(‘spinach’, ‘whole wheat’);
takeOrder(‘pepperoni’, ‘brooklyn style’);

console.log(getSubTotal(orderCount))
//

for loging the console of getSubTotal it returns itemCount which is 3 and multiply with 7.5.

Q.1 dose the itemCount represent the orderCount.
Q.2 if this, how the orderCount find out how many time i place takeOrder.
Q.3 What is the relation among the three thing of itemCount, orderCount and takeOrder, where i call getSubTotal(orderCount) and it returns total number of takeOrder * 7.5

answer will help me a lot.
thanks in advance.


#2

A.1 function parameters (itemCount) are placeholders until the function is called and a argument (orderCount) is supplied for the parameter, then the value of orderCount is passed to itemCount

this means values don’t have to be same every time:

const addFunction = (x, y) => {
    return x + y
}

console.log(addFunction(3,5))
console.log(addFunction(4,6))

I think for all questions, understanding how parameters and arguments works is crucial

other important things: functions don’t execute until called, that is why function parameters can be placeholders


#3

@megaplayer38319

  1. Yes
  2. In the const takeOrder() function at the top, the operation orderCount++ is acting as a counter. Every time takeOrder() is called at the bottom, for different pizzas, the orderCount++ adds 1 to the value. Since the value initially started as 0 (let orderCount= 0;), orderCount++ gets used 3 times, and has a final value of 3.
  3. itemCount is a placeholder for orderCount, and itemCount could be anything. I agree that it’s a bit confusing at this point in the tutorial since it’s never explicitly addressed. However, it could just as well have been orderCount again. getSubTotal() needs some parameter to generate a value, since it was established as undefined. Calling getSubTotal(orderCount); provides getSubTotal() with a value from our counter orderCount++; As I said earlier that 3 takeOrder() functions we called incremented the value to 3, so getSubTotal(orderCount) will now take 3 * 7.5 and return that value to getSubTotal in the console.

#4

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