Functions 5/7 Going From orderCount to itemCount Help


#1


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


So, I have seen this question asked before and for that I apologize for creating another similar post. However I do not understand the answer(s) that were given to the previous posts regarding this specific question. In 5/7, as seen below, there is a moment when we see "return itemCount * 7.5". And in this moment, itemCount has the value of three and I understand that itemCount is picking up the value from orderCount. However I wanted to know why itemCount obtains orderCount's value, when itemCount is never even mentioned until that moment.

In advanced, I appreciate your help.

Here is the code below for your reference


var orderCount = 0;

function takeOrder(topping, crustType) {
  orderCount = orderCount + 1;
  console.log("Order: " + topping + " and " + crustType + " pizza");
}

takeOrder("ham", "garlic");
takeOrder("chicken", "cheese");
takeOrder("sausage", "plain");

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


#2

You can read these links where I discussed about it


#3

Thank you for your response.

I hope this helps to clarify what I am not getting.:

I understand that orderCount is a global variable that is acting as a counter for whenever the function "takeOrder" is called. And I understand that the function "getSubTotal" takes in the value that we get from orderCount and puts it into "itemCount". What I am confused about is how/why the transfer from "orderCount" to "itemCount" takes place. Again, if you had explained why that transfer takes place or how we, as programmers, can know that the transfer will take place I apologize. However, I am trying to understand the logic behind this, instead of just saying "yes it happens that way because it happens that way." Thank you again for your response though.


#4

how/why the transfer from "orderCount" to "itemCount" takes place?

There are two types of fucntions.

  1. One with parameters
  2. Without parameters

The first kind of function is mostly used because It can process the data and return/print it.
What are you asking is already answered by me in question.

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

Can you tell what this function does?

It counts number of times order have done and multiply order by 7.5
You're asking how the transfer takes place.

Function working in javascript..

Function--

Functions are useful chunks of code that are used inside a program for many reasons.
1. Reusability
2. less bug
3. Modular coding

Function variables -- All parameters or .variable defined inside the function (with var keyword) is considered as local variable.

This function gives double of any number....

function double(numbr) {
    return numbr * 2;
}

I want to calculate double value of 10.

so I write..
double(10);
As of I can understand your question, You're asking how numbr is being replaced by 10 , right?
Because function is defined to do it that way.

try to just call double without any parameters and let me know the result..

double();

I hope it clears your doubt a bit more!! :slight_smile:


#5

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