Click to See Complete Forum and Search --> : Converting an Equation


Githlar
03-28-2004, 09:28 AM
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?

steelersfan88
03-28-2004, 09:31 AM
not sure why, but I can tell you to replace all of the eval(...) in your code with Number(...). What happens now?

Githlar
03-28-2004, 09:38 AM
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

steelersfan88
03-28-2004, 09:44 AM
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

David Harrison
03-28-2004, 09:45 AM
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);

steelersfan88
03-28-2004, 09:47 AM
or simply switch the of the equations!

Githlar
03-28-2004, 10:06 AM
hey thanx guys, now it works like a charm