Exercise 5 - Explain Logic


#1

Here’s the exercise I need some explanation on:
https://www.codecademy.com/courses/learn-javascript-functions/lessons/functions/exercises/return?action=lesson_resume&course_redirect=introduction-to-javascript

When looking at the code:

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;
};

console.log(getSubTotal(orderCount));

There are couple of things I’m struggling with:

  1. I can’t seem to understand how the code runs in regard to step by step. When it gets to orderCount++ at what stage it comes back to increment? The way I understand it is it should increment the orderCount++ only if the code comes back to that step, but the way I see it, the code just runs from top to bottom and never gets back to starting point to run again? I’m not sure if this makes sense what I’m writing, let me know if I should try and explain it any other way.

  2. I do not understand how the itemCount works and how it knows to count the takeOrder actions since it’s in no way connected to the above code?

  3. Finally, the last line of code, I don’t understand why both parenthesis must be included? Can any one please elaborate on that?

Thanks and sorry if it’s simple stuff to understand, just can’t seem to get it and I feel it’s quite important to grasp these concepts at this stage.

Cheers!


#2

that logic applies, but not when function are involved, every time you call the function:

// you call the function here 3 times
takeOrder('mushroom', 'thin crust');
takeOrder('spinach', 'whole wheat');
takeOrder('pepperoni', 'brooklyn style');

the function itself:

const takeOrder = (topping, crustType) => {
  orderCount++;
  console.log('Order: ' + crustType + ' pizza topped with ' + topping);
};

gets executed, so it does go “back” as you like to say

2 . its connected, here you declare the function with parameter itemCount:

const getSubTotal = (itemCount)

and then you call the function with argument orderCount:

console.log(getSubTotal(orderCount));

the argument at function call gets passed to the parameter, which you can prove:

const getSubTotal = (itemCount) => {
  console.log(itemCount)
  return itemCount * 7.5;
};

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

see that its the exact same value?

takeOrder has exactly the same parameter/argument construction, except the arguments (at function call) are strings, where as in getSubTotal the argument is a variable

3 . not sure what you mean, console.log() and getSubTotal() are function calls, calling a function requires parentheses. And yes, you can nest function calls


#3

Stetim,

Thanks so much for taking the time!

For my first question, now I understand what I have failed to grasp. This makes total sense now!

For my second question, I’m almost there, it’s just that I can’t seem to understand the principle behind console.log(getSubTotal(orderCount)); Does the multiplication happen in the console.log or the getSubTotal function?

If I console.log(orderCount); it prints number 3, which seems to suggest the price of pizza is not calculated in the function but rather in the console.log.

Disregard my third question, I seem to understand what I didn’t before.


#4

in the getSubTotal function, this function returns the multiplication and then console.log() logs this to the console

the only purpose of console.log() is to log to the console (the black output window on the right side)

no? console.log() only logs, it doesn’t calculate. But console.log() is the only way we can see what is happening, doesn’t mean the logic is occurring with console.log()


#5

Stetim,

I understand what you’re saying, but why if I console.log(getSubTotal) it doesn’t show the final result if that’s where the math is done? It should - no? Instead it has to be console.log(getSubTotal(orderCount); ?

Sorry for torturing you.


#6

because getSubTotal is a function, to execute a function, we need to call it, which requires parentheses:

getSubTotal()

give the function has a parameter, we also need to supply argument at function call (to satisfy function parameter), as explained

the soul purpose of console.log() is to logs things to the console, its the only thing the function does

given getSubTotal returns the result, we can call console.log() on the function call (getSubTotal(orderCount)) to log the returned result to the console


#7

Stetim,

I think I found the problem why I have trouble understanding what you’re saying. Every thing you said is correct, but the exercise file has a mistake.

If you take a look at my example in the first post the code is:

let **order**Count = 0;

and the function getSubTotal asks to return the **item**Count not orderCount although the exercise doesn’t allow me to use the correct name orderCount - that’s exactly the reason why I was not able to understand how this code is linked to the previous code.

Geez, sorry for brain@#$%ing you. It makes sense now.


#8

no, clearly it doesn’t make sense. return itemCount is correct.

lets simplify:

// function declaration with parameter itemCount
const getSubTotal = (itemCount) => {
  console.log(itemCount);
  console.log(itemCount * 7.5)
};

// function call with argument orderCount
getSubTotal(orderCount)

see the comments in the code, so you understand that the parameter (itemCount) is defined at function declaration, and the argument (orderCount) is passed at function call

When you call the function, the value of the argument gets copied into the function parameter, if you like it visualize it, this is what is happening at function call:

itemCount = orderCount

a function parameter is only a placeholder until it gets value from an argument at function call

function only execute when called.

what so far is still surprising me (you are not the first) is that with the takeOrder function declaration and takeOrder function calls:

// function declaration with parameters topping and crustType
const takeOrder = (topping, crustType) => {
  orderCount++;
  console.log('Order: ' + crustType + ' pizza topped with ' + topping);
};

// three function calls with string arguments
takeOrder('mushroom', 'thin crust');
takeOrder('spinach', 'whole wheat');
takeOrder('pepperoni', 'brooklyn style');

the parameter and arguments are seemingly understood fine


#9

Thanks for your feedback. After I wrote the reply I tested what I suggested and it didn’t work, as you suggested.

I kind of understand what you’re saying here:

see the comments in the code, so you understand that the parameter (itemCount) is defined at function declaration, and the argument (orderCount) is passed at function call

When you call the function, the value of the argument gets copied into the function parameter, if you like it visualize it, this is what is happening at function call:

and I’ll just have to live with it, but you can kill me, but I don’t understand why orderCount gets passed into itemCount

I’m just thinking if I have a function relating to some thing else in the program, not relevant to counting orders, it would for some weird reason copy what’s in the orderCount and I don’t understand why or how…

I understand this part:

Function get’s called, value is stored in takeOrder … but there’s no command line for getSubTotal for any of the above values to be saved in there. How can itemCount know what’s stored in orderCount? Or why would it understand that functions were called 3 times?


#10

To understand why we use itemCount, you must first understand why we use parameters.

As of defining the function, the program does not know whether itemCount is a number, a string, a list, a dictionary, etc. As far as the program is concerned itemCount could be anything and, well, it could care less. It’s just a placeholder for whatever you pass into the function when you actually call it. In other words think of parameters like variables. Whenever you call a function, the argument you pass into it becomes the value for itemCount. For instance if you called:

getSubTotal(orderCount);

You would essentially be telling the program to run getSubTotal(itemCount) except now, itemCount = orderCount. Until a function is actually called, the parameters you have set are valueless placeholders. Once you call the function and pass in arguments for those parameters, they take on value and purpose. What I mean by this is, if you did the following:

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;
};

getSubTotal(orderCount);

Notice how I defined the function using itemCount as a parameter and then I called the function using orderCount as an argument. This is telling the program to run the code within getSubTotal(itemCount) but replace all mentions of itemCount with orderCount. Basically it does this:

return orderCount * 7.5;

This is why we use itemCount as we do in the function.


#11

Ok, this seems to give me a better idea how it works. I think I need to go practice a bit for every thing to sink in.

Is there a third party app which is good for running javascript and visible outputs - not just codecademy in the browser?
And I hope I’m not asking too much, are there websites that have more exercises to practice this concept?

One more thing, thanks for the great support. I’m sure I wouldn’t have grasped it if it weren’t for stetim and aquaphoenix. I appreciate your patience.


#12

There are plenty such websites/apps out there, one need only consult google to find them…


#13

Yes, I’ve seen websites, but was hoping an app for offline use. Never mind, I’ll google it. Thanks!


#14

look at this code:

const example = (parameter){
    console.log(parameter);
}

example("argument")

the "argument" at function call gets copied into the function parameter (which i named parameter to be convenient). This just happens, and needs to be accepted. Its how function works

what value? This seems to be where the confusion is coming from.

function parameters are placeholders, they get there values from the argument at function call.

There is nodeJS, once installed it can be used to run JS code:

https://nodejs.org/en/

the alternative is to use the browser, by making a local html and JS file you can cause output in the console (which is included in a web-browser) or to the webpage. Given the files are stored locally, they are read from hard hard drive so don’t require internet connection


#15

:face_with_raised_eyebrow:

I literally just explained this… lol :wink:


#16

actually, i was just repeating myself: (sometimes repeating helps)

i already mentioned it earlier (even before your reply)


#17

I know, I was just being jocular. :stuck_out_tongue_closed_eyes:


#18

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