如果elses不运行

时间:2017-12-09 01:49:44

标签: javascript

我正在尝试让我的代码为我的游戏调整变量。然而,即使currWeather不等于“非常热”,它也只运行第一个if语句。它应检查当前的天气,然后根据if语句调整正确的变量。

function checkWeather(){
            if(currWeather = "Very Hot"){
                health -= 8;
                milesPerDay *= 0.7;
                console.log("subtracted");
           }
           else if(currWeather = "Hot"){
                health -= 3;
                milesPerDay *= 0.9;
                console.log("subtracted");
           }
           else if(currWeather = "Warm"){
                health += 1;
                milesPerDay *= 1;
                console.log("subtracted");
           }
           else if(currWeather = "Cool"){
                health += 1;
                milesPerDay *= 0.95;
                console.log("subtracted");
           }
           else if(currWeather = "Cold"){
                health -= 5;
                milesPerDay *= 0.8;
                console.log("subtracted");
           }
           else if(currWeather = "Very Cold"){
                health -= 12;
                milesPerDay *= 0.7;
                console.log("subtracted");
           }
           else if(currWeather = "Rain"){
                health -= 4;
                milesPerDay *= 0.6;
                console.log("subtracted");
           }
           else if(currWeather = "Heavy Rain"){
                health -= 8;
                milesPerDay *= 0.4;
                console.log("subtracted");
           }
           else if(currWeather = "Snow"){
                health -= 15;
                milesPerDay *= 0.3;
                console.log("subtracted");
           }
           else if(currWeather = "Blizzard"){
                health -= 30;
                milesPerDay *= 0.1;
                console.log("subtracted");
           }
           else if(currWeather = "Heavy Fog"){
                health -= 3;
                milesPerDay *= 0.5;
                console.log("subtracted");
           }
           else{
            health += 0;
            milesPerDay *= 1;
            console.log("ez");

           }
           console.log("test");

        }

1 个答案:

答案 0 :(得分:1)

=用于分配; ==是为了平等。更好的是,使用===进行严格比较。

=运算符在赋值后返回变量的值。然后,该值被解释为true(Truthy values)