我的JS代码有问题。我制作了两个具有相同值的对象。我想确定哪个人的BMI较高。我做了if语句,但我认为控制台应记录“相同的BMI”。它告诉我Mike的BMI较高。
var mark = {
fullName: 'Mark',
mass: 92,
height: 1.93,
bmi: function(){
return this.mass/(this.height * this.height);
}
};
var mike = {
fullName: 'Mike',
mass: 92,
height: 1.93,
bmi: function(){
return this.mass/(this.height * this.height);
}
};
var higherBMI = mark.bmi() > mike.bmi();
if (higherBMI === true)
{
console.log('Mark has higher BMI. He has ' + mark.bmi());
} else if (higherBMI === false){
console.log('Mike has higher BMI. He has ' + mike.bmi());
} else{
console.log('The same BMI');
}
答案 0 :(得分:4)
如果mark.bmi不高于mike.bmi,则您要分配给test_that_fails (__main__.MyTest) ... expected failure
test_that_passes (__main__.MyTest) ... unexpected success
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK (expected failures=1, unexpected successes=1)
的{{1}}运算符将返回false,然后在测试>
时,higherBMI为false是正确的。这就是为什么要进入该块并记录“ Mike具有更高的BMI ...”的原因,您需要测试higherBMI
,然后如果两者都不为真;不高不低就等于它。