当使用=
作为TDEE计算控制流比较运算符时,流程将停在if (activityLevel = 'sedentary')
。当我使用==
时,它按预期工作。我已经坚持了2个小时并且我已经阅读了3篇文章,但我无法弄清楚。
我知道==
用于相同的值和含义,===
用于相同的值和类型,但我不认为这与它有任何关系。我想也许=
用于类型,在这种情况下如果activityLevel
等于一个字符串,但我很确定那不是因为我没有遇到过在=
的文档中。
注意:TDEE控制流程中的注释数字是应该的结果,而不是它们的结果。
// REE Calculation
// Males - 10 x weight (kg) + 6.25 x height (cm) – 5 x age (y) + 5 = REE
// Females - 10 x weight (kg) + 6.25 x height (cm) – 5 x age (y) – 161 = REE
const gender = 'male'; // prompt('Are you male or female?');
const weight = 100; // prompt('How much do you weigh in KG?');
const height = 185; // prompt('What is your height in cm?');
const age = 23; // prompt('What is your age');
if (sex = 'male') {
stepOne = 10 * weight;
stepTwo = 6.25 * height;
stepThree = 5 * age + 5;
var ree = stepOne + stepTwo - stepThree;
}
if (sex = 'f') {
stepOne = 10 * weight;
stepTwo = 6.25 * height;
stepThree = 5 * age - 161;
var ree = stepOne + stepTwo - stepThree;
}
console.log(ree.toFixed(0)) // Answer is correct - 2171
// TDEE Calculation
var activityLevel = 'moderate activity' // prompt('What is your activity level?\nsedentary/light activity/moderate activity/very active');
if (activityLevel = 'sedentary') {
var tdee = ree * 1.2; // 2642.40
} else if (activityLevel = 'light activity') {
var tdee = ree * 1.375; // 3027.75
} else if (activityLevel = 'moderate activity') {
var tdee = ree * 1.55; // 3413.10
} else { // 3798.45
var tdee = ree * 1.725;
}
console.log(tdee.toFixed(0))
答案 0 :(得分:1)