Reputation: 3
So I'm trying to take two values, sum them and then multiply by 250. The first value is a negative number and the second is positive. For example:
-Hunger + Hunger2 * 250 =
I'm new to coding like this and not sure what I have messed up. Can someone please provide some insight?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>ARK STAT CALCULATOR</title>
</head>
<body>
<form id="stat" action="">
<fieldset>
<legend>ARK STAT Calculator</legend>
<p>
<label for="Health">Base-Health</label>
<input id="Health" name="Health" type="number" />
<label for="Health2">Next Value</label>
<input id="Health2" name="Health2" type="number" />
</p>
<p>
<label for="Stamina">Base-Stamina</label>
<input id="Stamina" name="Stamina" type="number" />
<label for="Stamina2">Next Value</label>
<input id="Stamina2" name="Stamina2" type="number" />
</p>
<p>
<label for="Oxygen">Base-Oxygen</label>
<input id="Oxygen" name="Oxygen" type="number" />
<label for="Oxygen2">Next Value</label>
<input id="Oxygen2" name="Oxygen2" type="number" />
</p>
<p>
<label for="Food">Base-Food</label>
<input id="Food" name="Food" type="number" />
<label for="Food2">Next Value</label>
<input id="Food2" name="Food2" type="number" />
</p>
<p>
<label for="Weight">Base-Weight</label>
<input id="Weight" name="Weight" type="number" />
<label for="Weight2">Next Value</label>
<input id="Weight2" name="Weight2" type="number" />
</p>
<p>
<label for="Melee">Base-Melee Damage</label>
<input id="Melee" name="Melee" type="number" />
<label for="Melee2">Next Value</label>
<input id="Melee2" name="Melee2" type="number" />
</p>
<p>
<label for="Speed">Base-Movement Speed</label>
<input id="Speed" name="Speed" type="number" />
<label for="Speed2">Next Value</label>
<input id="Speed2" name="Speed2" type="number" />
</p>
<p>
<input type="submit" value="CALCULATE MAX STATS" />
or
<input type="reset" value="Reset" />
</p>
<p>
<label for="stat">Health</label>
<input id="stat" name="stat" type="number" />
</p>
<p>
<label for="stat2">Stamina</label>
<input id="stat2" name="stat2" type="number" />
</p>
<p>
<label for="stat3">Oxygen</label>
<input id="stat3" name="stat3" type="number" />
</p>
<p>
<label for="stat4">Food</label>
<input id="stat4" name="stat4" type="number" />
</p>
<p>
<label for="stat7">Weight</label>
<input id="stat7" name="stat7" type="number" />
</p>
<p>
<label for="stat5">Melee Damage</label>
<input id="stat5" name="stat5" type="number" />
</p>
<p>
<label for="stat6">Movement Speed</label>
<input id="stat6" name="stat6" type="number" />
</p>
</fieldset>
</form>
<script>
(function () {
function calculateStat(Health,Stamina,Oxygen,Food,Weight,Melee,Speed,Health2,Stamina2,Oxygen2,Food2,Weight2,Melee2,Speed2) {
Health = parseFloat(Health);
Health2 = parseFloat(Health2);
return (Health + Health2 * 250);
Stamina = parseFloat(Stamina);
Stamina2 = parseFloat(Stamina2);
return (Stamina + Stamina2 * 250);
Oxygen = parseFloat(Oxygen);
Oxygen2 = parseFloat(Oxygen2);
return (Oxygen + Oxygen2 * 250);
Food = parseFloat(Food);
Food2 = parseFloat(Food2);
return (Food + Food2 * 250);
Weight = parseFloat(Weight);
Weight2 = parseFloat(Weight2);
return (Weight + Weight2 * 250);
Melee = parseFloat(Melee);
Melee2 = parseFloat(Melee2);
return (Melee + Melee2 * 250);
Speed = parseFloat(Speed);
Speed2 = parseFloat(Speed2);
return (Speed + Speed2 * 250);
}
var stat = document.getElementById("stat");
if (stat) {
stat.onsubmit = function () {
this.stat.value = calculateStat(this.Health.value, this.Health2.value);
this.stat2.value = calculateStat(this.Stamina.value, this.Stamina2.value);
this.stat3.value = calculateStat(this.Oxygen.value, this.Oxygen2.value);
this.stat4.value = calculateStat(this.Food.value, this.Food2.value);
this.stat5.value = calculateStat(this.Melee.value, this.Melee2.value);
this.stat6.value = calculateStat(this.Speed.value, this.Speed2.value);
this.stat7.value = calculateStat(this.Weight.value, this.Weight2.value);
return false;
};
}
}());
</script>
</body>
</html>
Upvotes: 0
Views: 55
Reputation: 30416
Your function is completely off, you have multiple returns statements (which is fine except your function will complete executing after it encounters the first one) and it takes 14 argument but you are only passing the first 2 when you call it. I would start with fixing this first. An interesting side effect of how JavaScript handles function arguments means your misuse is hiding the problems with your function. This function sould fit your needs:
function calculateStat(a, b) {
return (parseFloat(a) + parseFloat(b) * 250);
}
Now as for the order of operations, JavaScript interprets this equation as a + (b * 250)
(2 + 3 * 4 == 14
) and not as (a + b) * 250
((2 + 6) * 4 == 20
), so if this is what you are looking for just change it to this: (parseFloat(a) + parseFloat(b)) * 250
Upvotes: 1