添加值和乘法

时间:2017-03-03 17:14:55

标签: javascript html5

所以我试图取两个值,求它们然后乘以250.第一个值是负数,第二个值是正数。例如:

-Hunger + Hunger2 * 250 =

我是这样编码的新手,不知道我搞砸了什么。有人可以提供一些见解吗?

<!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>

1 个答案:

答案 0 :(得分:1)

你的函数是完全关闭的,你有多个返回语句(除了你的函数在遇到第一个之后完成执行时很好)并且它需要14个参数但你只是在调用它时传递前2个。我首先要解决这个问题。 JavaScript如何处理函数参数的一个有趣的副作用意味着您的滥用会隐藏您的函数问题。这个功能可以满足您的需求:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?site=$1 [QSA,L]

现在关于操作的顺序,JavaScript将此等式解释为function calculateStat(a, b) { return (parseFloat(a) + parseFloat(b) * 250); } a + (b * 250))而不是2 + 3 * 4 == 14(a + b) * 250),所以如果这就是你的意思正在寻找改变它:(2 + 6) * 4 == 20