Functions 5/7


#1

Why "itemCount * 7.5" instead of "orderCount * 7.5" How does java know what an "itemCount" is?


#2

We don't know unless you post your code and a link to the exercise :wink:


#3

Learn Javascript
Functions
5/7

var orderCount=0;

function takeOrder(topping,crustType){
console.log('Order: '+crustType+' crust pizza with

'+topping);
orderCount=orderCount+1;
}
function getSubTotal(itemCount){
return itemCount*7.5;
}
takeOrder('bacon','thin');
takeOrder('peperroni','medium');
takeOrder('chicken','thick');
console.log(getSubTotal(orderCount));


#4

I'm doing exactly the same exercise. I think I've kind of "solved it". Let me try and help:

in your last line of code, "console.log(getSubTotal(orderCount));" , you are CALLING the function, and telling it: "Apply orderCount as the new parameter for the function getSubTotal", since orderCount is the argument.

Which means that now itemCount has become orderCount (and since orderCount has a value of 3, because we have 3 orders, itemCount now also has a value of 3, and thus, since 7.5*3 equals 22.5m, thats is why you get your result)

I'm a newbie just like you, I tried to explain it to the best of my abilities. I'm currently struggling in Functions 6/7, so yeah, it gets even more hellish!


#5

First things first: this is Javascript , not Java - there's a difference :stuck_out_tongue:

Second: As I don't know what your error message was, I just ran your code through Chrome's dev tools console, and the only error that comes up is;

Uncaught SyntaxError: Invalid or unexpected token

That means something is in the wrong place. It didn't take me long to see it;

console.log('Order: '+crustType+' crust pizza with

'+topping);

The problem is in this part, and it's the blank line here. All of this should be in one line,

console.log('Order: ' + crustType + ' crust pizza with' + topping);

else Javascript will get confused :slight_smile:


#6

Thanks, that's what I wanted to Know. Good luck on your progress.


#7

That wasn't my question, I didn't state I got an error message. Thanks anyway I appreciate the response.


#8

Wow, thank you for that explanation, and thank you to @dchaka for asking the very question I had.
I was banging my head on the desk trying to figure it out too. I kinda get it now, although it seems like it could've been explained better in the conclusion to the instructions. I think this must be a very confusing point for most people going through this course if they're really trying to understand how functions work.

I can't help but think the same output could have been achieved in a simpler-to-understand program, but I'm way too new at this to detail how.


#9

A post was split to a new topic: Functions 5/7


#10