Boby
Boby

Reputation: 77

Function to modify a variable not working - Javascript

My issue concern 3 variables :

var modifyForce = 2;
var forceBase = 15;
var Force = (forceBase + modifyForce);

It display 17 Force

The first function which is working is :

{
    Force++;
    document.getElementById("Force").innerHTML = Force;
}

It adds +1 to Force each time I call it

But the second function :

{
    forceBase++;
    document.getElementById("Force").innerHTML = Force;
}

Does not add +1 to Force. Since it should add +1 to forceBase and Force = forceBase + modifyForce then it should add 1 to Force... I think.

My goal is to have a "forceBase" statistic + a "modifyForce" in order to get a total of "Force"

I am probably missing something simple but I can't find what. :/

Thank you :)

Upvotes: 1

Views: 89

Answers (5)

David P
David P

Reputation: 2083

The best thing might be to create a class:

var objForce = {
    modifyForce: 2,
    forceBase: 15,
    Force: 17,
    reCalculate: function() { 
        this.Force = this.modifyForce + this.forceBase;
    }
};
objForce.forceBase++;
objForce.reCalculate();
document.getElementById("Force").innerHTML = objForce.Force;

Upvotes: 1

fortiZ
fortiZ

Reputation: 69

var modifyForce = 2;
var forceBase = 15;
var Force = (forceBase + modifyForce);

Force is one time calculated with forceBase + modifyForce After the declaration you just have the number saved in Force

If you would like to use Force as a function to add modifyForce to forceBase use something like

var Force = function () {
    return forceBase + modifyForce;
}

but you can't increment it like Force++ :P

Upvotes: 2

Chris Charles
Chris Charles

Reputation: 4446

No one seems to have suggested this yet...

If you want Force to always be forceBase + modifyForce then make it a function.

function Force(){
    return forceBase + modifyForce
}

Which now makes this work (note Force is now called with ():

{
    forceBase++;
    document.getElementById("Force").innerHTML = Force();
}

Upvotes: 2

Philipp
Philipp

Reputation: 11341

The code-block where you edit forceBase does not re-calculate the updated value of Force.

To make this simple I recommend you move the re-calcultation and output into a distinct function like this:

var modifyForce = 2;
var forceBase = 15;

function updateForce() {
    var Force = (forceBase + modifyForce);
    document.getElementById("Force").innerHTML = Force;
}

// Output the current value of Force
updateForce(); // output is 17

forceBase++;
updateForce(); // output is 18

modifyForce++;
updateForce(); // output is 19

Upvotes: 1

David Reeve
David Reeve

Reputation: 871

The problem is you're not updating Force to be equal to the new forceBase + modifyForce. Beyond declaration, Force doesn't keep any connection to forceBase or modifyForce, and you have to manually assign the new value to it. You should change your code as such:

{
    forceBase++;
    Force = forceBase + modifyForce;
    document.getElementById("Force").innerHTML = Force;
}

Keep in mind that if Force is changed in another function (such as the first one you posted), this will reset those changes.

Upvotes: 2

Related Questions