0

My code increments a number up or down by 1. The problem is "testNum" which is a global variable should reflect the increments up or down but when I check the value of "testNum" in the console it's always 20. I pass "testNum" to my function and return it. Why isn't it reflecting the increments?

Fiddle: http://jsfiddle.net/FnUXw/1/

<input type="button" id="btnDown" value="-">
<span id="amountDiv">amount div</span>
<input type="button" id="btnUp" value="+">

<script>
var testNum = 20;

amountDiv.innerHTML = testNum;

function makeButtonIncrement(button, upDown, displayDiv, variable) {

    function changeValue () {
        if (upDown == "up") {
            variable++;
        }

        if (upDown == "down") {
            variable--;
        }

        displayDiv.innerHTML = variable;
    }

    button.onclick = changeValue;

    return variable;
}

makeButtonIncrement(document.getElementById('btnDown'), "down", document.getElementById('amountDiv'), testNum);

makeButtonIncrement(document.getElementById('btnUp'), "up", document.getElementById('amountDiv'), testNum);
</script>
user1822824
  • 2,478
  • 6
  • 41
  • 65

3 Answers3

4

Numbers are passed by value, not by reference so you can't do what you're trying to do that way. With your code structured the way it is, you could do it like this where you assign the return value:

testNum = makeButtonIncrement(document.getElementById('btnDown'), "down", document.getElementById('amountDiv'), testNum);

Only arrays and objects are passed by reference so they can be passed as arguments and have the function modify the original variable. Other types are passed by value (e.g. a copy is made for the argument) so you cannot modify the original.

The work-around to modify the original is to pass it as a property of an object. Then, because the object is passed by reference, the function can modify the original property value.


Or, since you're already using a global variable, you could just modify the global variable directly and not even pass it as an argument. It is the passing as an argument that makes a copy of it so you can't modify the original. Global variables are, in general, not recommended, but if it's already global for other reasons, you can just modify it directly in your function without passing it as an argument.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @JanDvorak - Correct term for what? – jfriend00 Feb 23 '13 at 21:20
  • for that is the passed value is either a primitive value or an object reference – John Dvorak Feb 23 '13 at 21:21
  • @JanDvorak - the normal programming terms I'm familiar with that apply to all languages are "pass by value" and "pass by reference". I suppose you could say that javascript has pass by object (array or object) which turns out to be pass by reference. – jfriend00 Feb 23 '13 at 21:23
  • @JanDvorak - in your article, it says: 'However, the term "call by sharing" is not in common use'. – jfriend00 Feb 23 '13 at 21:37
0

You are using "variable++" and "variable--" which is incrementing or decrementing that locally scoped variable (local to the function scope). The parameter is not a pointer to the global variable, it is passed by value (not by reference).

The simplest modification would be to change variable++ and variable-- to testNum++ and testNum-- respectively and remove that final function parameter as it is unnecessary.

Rob M.
  • 35,491
  • 6
  • 51
  • 50
0
function makeButtonIncrement(button, upDown, displayDiv) {

    function changeValue () {
        if (upDown == "up") {
            testNum++;
        }

        if (upDown == "down") {
            testNum--;
        }

        displayDiv.innerHTML = testNum;
    }

    button.onclick = changeValue;
}
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80