/    Sign up×
Community /Pin to ProfileBookmark

Converting an Equation

Ok, I don’t know a whole bunch about javascript, but would you say that this:

function calculatecombatlevel(){
var combatlevel = eval(document.form.attack.value)/4 + eval(document.form.strength.value)/4 + eval(document.form.defense.value)/4 + eval(document.form.hitpoints.value)/4 + eval(document.form.prayer.value)/8 + eval(document.form.magic.value)/8
var targetcombat = eval(document.form.targetcombat.value)*4 – eval(combatlevel)*4
var pmc = eval(document.form.targetcombat.value)*8 – eval(combatlevel)*8
}

is equal to this:

combatlevel = (att/4)+(str/4)+(def/4)+(hits/4)+(pray/8)+(mag/8);
targetcombat = (targetcombat*4)-(combatlevel*4);
pmc = (targetcombat*8)-(combatlevel*8);

it seems right, but for pmc I keep getting a negative number, can someone tell me what’s wrong?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Mar 28.2004 — not sure why, but I can tell you to replace all of the eval(...) in your code with Number(...). What happens now?
Copy linkTweet thisAlerts:
@GithlarauthorMar 28.2004 — Well, see I am trying to turn the javascript, which works perfect, into a mathematical equation for a program that I am making. In my formula that I converted it

(

combatlevel = (att/4)+(str/4)+(def/4)+(hits/4)+(pray/8)+(mag/8);

targetcombat = (targetcombat*4)-(combatlevel*4);

pmc = (targetcombat*8)-(combatlevel*8);

)

to it works perfect, exept for pmc which always comes out as a negative number
Copy linkTweet thisAlerts:
@steelersfan88Mar 28.2004 — you might consider seitching the orde rof your equations to:

  • 1. First Equation

  • 2. Third Equation

  • 3. Second Equation


  • This results because you are aissigning the difference back into the targetcombat variable.

    You are then taking this small amount and subtracting a large amount, which makes it negative, example:

    targetcombat=5

    combatlevel=4

    targetcombat=(4*7)-(4*6), = 4

    pmc = (8*4)-(8*6), = -16
    Copy linkTweet thisAlerts:
    @David_HarrisonMar 28.2004 — var targetcombat = eval(document.form.targetcombat.value)*4 - eval(combatlevel)*4

    Old pmc:

    var pmc = eval(document.form.targetcombat.value)*8 - eval(combatlevel)*8

    New pmc:

    pmc = (targetcombat*8)-(combatlevel*8);

    You are replacing document.form.targetcombat.value with targetcombat. Unfortunately the two are not equal to each other. Your new code should look like this:

    pmc = (targetcombat*8)-(combatlevel*4);
    Copy linkTweet thisAlerts:
    @steelersfan88Mar 28.2004 — or simply switch the of the equations!
    Copy linkTweet thisAlerts:
    @GithlarauthorMar 28.2004 — hey thanx guys, now it works like a charm
    ×

    Success!

    Help @Githlar spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...