1

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 :)

1
  • 1
    You need to manually update Force, there is no references. Commented Feb 11, 2015 at 13:52

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@NaeemShaikh What in the world are you talking about?.
op thinks force is already incremented say upto 20, then again reinitializing to Force = forceBase + modifyForce; will get it back to 12
@NaeemShaikh That is not the issue.... The issue is the fact the OP thinks that when you add two numbers, they will automatically update the calculation when either of the two numbers is increased/decreased.
2

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

1 Comment

and there is no need for it to. @R3tep
2
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

Comments

1

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

Comments

1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.