我正在阅读有关JavaScript的一系列教程。其中一个让我在JS中构建BMI计算器,同时检查结果。它还提供了一个解决方案并进行了解释。就是这样:
function bmiCalculator(weight, height) {
var bmi = Math.round(weight / Math.pow(height, 2));
console.log("Your BMI is " + bmi);
return bmi;
}
var bmi = bmiCalculator(65, 1.8);
console.log(bmi);
这个想法是,如果我将值弹出到bmiCalculator的var = bmi行中,以在控制台中获得输出。它给了我65和1.8的尝试,告诉我我的输出应该是20。好吧,它工作了(万岁),而我(想到了?)我不知道那里发生了什么。
随后的任务要求我对其进行修改,以便它结合条件语句并根据计算出的BMI输出某种“您体重不足/正常/体重超标”消息。它提供了以下内容的起始代码块:
function bmiCalculator (weight, height) {
return interpretation;
}
问题是本课程没有后续内容。没有解决方案(或解释)。有一个“检查代码”选项,但它只返回错误,我不确定出什么问题。
我想出了以下代码块:
function bmiCalculator(weight, height) {
var bmi = Math.round(weight / Math.pow(height, 2));
return interpretation;
}
var interpretation = bmiCalculator(65, 1.8);
if (bmi <= 18.5) {
alert("Your BMI is " + bmi + " so you are underweight.");
}
if (bmi > 18.5 && bmi <= 24.9) {
alert("Your BMI is " + bmi + " so you have a normal weight.");
}
if (bmi > 24.9) {
alert("Your BMI is " + bmi + " so you are overweight.");
}
它没有给我一个起始值,因此我只使用了上一个示例中的65和1.8。但是它返回给我的错误包括:
Your solution is incorrect
bmiCalculator() should return underweight for 6, 2
Expected undefined to equal 'Your BMI is 15, so you are underweight.'.
好吧,如果我在计算中输入6,2,我就不会得到15,我会得到2(我通过最初的bmiCalculator函数运行它作为测试。所以这对我来说没有意义。 此外,在给我的消息旁边还有一条便条:
ReferenceError: Can't find variable: bmi
我认为我在函数中定义了bmi。应该以其他方式定义它吗?
无论如何,由于未提供此任务,因此我想获得一种解决方案,因此在继续之前,我可以了解我的错误操作。谁能告诉我正确的解决方案以及我做错了什么,以便我可以从中学到吗?
答案 0 :(得分:2)
您刚刚混入变量,应该是:
function bmiCalculator(weight, height) {
var interpretation = Math.round(weight / Math.pow(height, 2));
return interpretation;
}
var bmi = bmiCalculator(65, 1.8);
if (bmi <= 18.5) {
alert("Your BMI is " + bmi + " so you are underweight.");
}
if (bmi > 18.5 && bmi <= 24.9) {
alert("Your BMI is " + bmi + " so you have a normal weight.");
}
if (bmi > 24.9) {
alert("Your BMI is " + bmi + " so you are overweight.");
}
答案 1 :(得分:1)
function bmiCalculator(weight, height) {
var bmi = Math.round(weight / Math.pow(height, 2));
return bmi; //since bmi is the variable where you get the result, so you should return the same variable
}
var interpretation = bmiCalculator(65, 1.8); // here you can store the returned result in any variable, so name "interpretation" will work fine here
// but then you have to use "interpretation" everywhere else subsequently
if (interpretation <= 18.5) {
alert("Your BMI is " + interpretation + " so you are underweight.");
}
if (interpretation > 18.5 && interpretation <= 24.9) {
alert("Your BMI is " + interpretation + " so you have a normal weight.");
}
if (interpretation > 24.9) {
alert("Your BMI is " + interpretation + " so you are overweight.");
}
答案 2 :(得分:0)
由于您是新手并且正在学习JavaScript,因此我将给出一些建议作为答案。
未定义的期望值等于“您的BMI为15,所以您体重不足”。
用于检查interpretation
的代码不在您的函数范围内,因此没有收到值。
ReferenceError:找不到变量:bmi
函数外部的变量bmi
对函数不可见。
这两个问题均归因于JavaScript概念scope,您可以在MDN上阅读更多内容。
答案 3 :(得分:0)
您返回了interpretation
(未定义),以下代码可以解决问题:
function bmiCalculator(weight, height) {
return Math.round(weight / Math.pow(height, 2));
}
var bmi = bmiCalculator(65, 1.8);
// you can simplify the comparison using else if and else
if (bmi <= 18.5) {
alert("Your BMI is " + bmi + " so you are underweight.");
} else if (bmi <= 24.9) {
alert("Your BMI is " + bmi + " so you have a normal weight.");
} else {
alert("Your BMI is " + bmi + " so you are overweight.");
}
另外,您应该阅读documentation about JS scope
在JavaScript函数中声明的变量变为该函数的本地变量。这就是为什么在bmi
函数之外进行比较时未定义bmiCalculator