不能在if语句Javascript中为变量赋值

时间:2015-03-15 22:17:44

标签: javascript if-statement variable-assignment

我在Javacsript编写一个函数,它返回两个变量,Consume和Burn。这是功能:

function calories(goal, goalWeight, activityLevel, days, gender, age, height, weight){
                var bmr=0;
                var consume=0;
                var burn=0;
                if (gender==='F'){
                    bmr = 655 + (4.3*weight) + (4.7*height) - (4.7*age);
                }
                if (gender==='M'){
                    bmr = 66 + (6.3*weight) + (12.9*height) - (6.8*age);
                }
                if (activityLevel==='Inactive'){
                    consume=bmr+(0.2*bmr);
                }
                if (activityLevel==='Relatively Active'){
                    consume=bmr+(0.3*bmr);
                }
                if (activityLevel==='Very Active'){
                    consume=bmr+(0.4*bmr);
                }
                if (goal==='Lose'){
                    if((goalWeight/days)<=0.06){
                        consume=consume-200;
                    }
                    if((goalWeight/days)<=0.15){
                        consume=consume-100;
                    }
                    if (activityLevel==='Inactive'){
                        burn=250;
                    }
                    if (activityLevel==='Relatively Active'){
                        burn=150;
                    }
                    if (activityLevel==='Very Active'){
                        burn=100;
                    }
                }
                if(goal==='Gain'){
                    if((goalWeight/days)<=0.06){
                            consume=consume+200;
                        }
                    if((goalWeight/days)<=0.15){
                        consume=consume+100;
                    }
                    if (activityLevel==='Inactive'){
                        burn=200;
                    }
                    if (activityLevel==='Relatively Active'){
                        burn=100;
                    }
                    if (activityLevel==='Very Active'){
                        burn=100;
                    }
                }
                if(goal==='Maintain'){
                    burn=100;
                }
                consume=Math.round(consume);
                window.alert(consume);
                window.alert(burn);
                return [consume,burn];
            }

在窗口框中,函数每次都返回正确的消耗值,但由于某种原因,刻录总是返回为零。如果我没有在if语句之外声明它,则返回为undefined。是否有某些原因我无法在if语句中为此变量分配数字?我已经使用window.alert()来测试是否正在访问正确的if语句,并且每次返回我想要的内容,但是从不为burn赋值,我不知道为什么!感谢您的任何建议

EDIT 谢谢你到目前为止的答案。输入的示例是:

var calories = calories(goal, healthyWeight, activityLevel, days, gender, age, height, weight);

其中:

goal = 'Lose'
healthyWeight = 128
activityLevel = 'Inactive'
days = 180
gender = 'M'
age = 20
height = 63
weight = 160

此返回消耗为预期的2101,并返回刻录为0

目前我所做的所有返回值都是:

window.alert(consume);
window.alert(burn);

测试它们的值,但一旦功能正常,我将使用

var consume=calories.consume;
var burn=calories.burn;
document.getElementById("consume").innerHTML = consume;
document.getElementById("burn").innerHTML = burn;

在功能之外,在浏览器中显示它们

2 个答案:

答案 0 :(得分:1)

return [consume,burn];将完全按照您的要求执行操作,因此当您调用calories(...)函数然后访问结果时,您可能会出错。

你应该这样做:

var result = calories(...);
console.log(result[0]);     // consume value
console.log(result[1]);     // burn value

答案 1 :(得分:0)

可能主要原因是您没有为您的功能提供适当的输入。请提供示例输入。

你应该使用if-else语句,例如

if (gender==='F') {
  bmr = 655 + (4.3*weight) + (4.7*height) - (4.7*age);
} else {
  bmr = 66 + (6.3*weight) + (12.9*height) - (6.8*age);
}

或if-else if-else结构,在其他情况下带警报 - 当您的输入与您的ifs不匹配时

if (gender==='F') {
  bmr = 655 + (4.3*weight) + (4.7*height) - (4.7*age);
} else if (gender==='M' {
  bmr = 66 + (6.3*weight) + (12.9*height) - (6.8*age);
} else {
  window.alert('wrong gender: ' + gender);
}