gpt4 book ai didi

javascript - 字符串等于字符串控制流

转载 作者:行者123 更新时间:2023-12-03 04:10:21 24 4
gpt4 key购买 nike

当使用 = 作为 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))

最佳答案

如果你想获取某物的类型,你可以使用 typeof运算符(operator)。当您在 if 语句中使用赋值运算符 (=) 时,它始终*会通过条件。如果您想检查相等性,您应该使用 ===== 运算符。

例如,如果您想检查事件级别,可以通过以下方式执行:

var activityLevel = 'moderate activity';
if (activityLevel === 'moderate activity'){
console.log("Moderate Activity");
}

This是关于 JS 中不同运算符的好文档。

<小时/>

更新:还注意到变量sex未定义。您在开始时定义了gender,但您正在检查变量sex

关于javascript - 字符串等于字符串控制流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44359625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com