Question on Return ||


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

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

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

It does not behave incorrectly, I don’t think. My question is: we create the function “const getSubTotal = (itemCount) => {
return itemCount * 7.5”

This interacts with orderCount ++; in order to return the number of pizzas. How does it now to multiply 7.5 by the orderCount if we never link itemCount and orderCount anywhere else?

Second question: with the code:

"let orderCount = 0;

const takeOrder = (topping, crustType) => {
orderCount++;"

Is the reason orderCount++ working because it’s in the function block of takeOrder? If we place it in that function block we don’t need to define what orderCount is?

This is myfirst time learning any program language and I am confused as ■■■■. Please help! :slight_smile:

```

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

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

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

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

<do not remove the three backticks above>

#2

“Is the reason orderCount++ working because it’s in the function block of takeOrder? If we place it in that function block we don’t need to define what orderCount is?”

What I meant to say is do we need to define how it relates to the variable inside of takeOrder?

I feel like my head is going in circles. I apologize if these questions don’t make any sense.


#3

we do link orderCount and itemCount:

// function declaration with parameter itemCount
const getSubTotal = (itemCount) => {
return itemCount * 7.5;
};
// function call with argument orderCount
getSubTotal(orderCount)

see the comments, i am going to use those terms i specified there.

when we call the function, we supply an argument (orderCount) the value of which gets passed to the function parameter (itemCount), that is how the “linking” works, except in your case the function call is inside another function, the above example is a simplified program of your program

orderCount++; is a shorthand, fully written its orderCount = orderCount + 1, so we have to define orderCount first, otherwise its undefined when we attempt orderCount + 1

orderCount has a global scope, all parts (including functions) can access it and update the variable


Pizza.js
#5

OH.MY.GOD…

You, stetim94, have just made the light bulb go off in my head! THANK YOU SO MUCH!


#6

I would also like to check my understanding if you wouldn’t mind.

In the code below we set the variable orderCount to 0. Within the function takeOrder we write orderCount++ so that every time the takeOrder function is called we add +1 to the variable orderCount. In this case orderCount would be 3.

   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');

#7

seems your understanding is fine now :slight_smile:


#8

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