Problem with incrementing


#1

Hi. I am learning Javascript currently, and I input this code:

orderCount=0;

function takeOrder(ordercount){
ordercount++;
}

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

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

I set the takeOrder function to increment its parameter by 1 (as seen in the function definition). However, when I run the code, the parameter does not increment. The console.log for orderCount stays put at zero (the value i assigned at the beginning), and I do not know why.

Please could you help with this?

Thanks very much,

Frederick.


#2

function parameters have a local scope (they only exist inside the function), so the global variable doesn’t change


#3

Thanks so much for the reply.

So how do I modify the program to increment the variable orderCount?

I tried putting the console log statement inside the function, but even with that, the ordercount does not show an increment. Shouldn’t it print the incremented version, since


#4

If you want to update a global variable, you shouldn’t have a local variable with the same name


#5

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