简体   繁体   English

我在哪里做错了? JS

[英]where am i doing wrong? JS

I am newbie at the Javascript and i just trying to learning functions it's a little bit hard but i do my best:).我是 Javascript 的新手,我只是想学习功能,这有点难,但我尽力了:)。 I am doing a function to calculate bmi like this;我正在做一个 function 来计算 bmi 像这样;

 let calculateWeight = parseFloat(prompt("Enter your weight", )) let calculateHeight = parseFloat(prompt("Enter your height?")) function calculateBmi(weight, height) { let bmi = weight / (height * height) if (bmi < 18.5) { console.log("You are underweight") } else if (bmi > 18.6) { console.log("You are normal weight") } else if (bmi > 25) { console.log("you are overweigh") } return bmi; } console.log(calculateBmi(calculateWeight, calculateHeight))

if i enter my values 72 weight and 1.80 kilos it says you are normal weight yeah it's correct but whenever i enter my values >25 it's says again you are normal weight where am i doing wrong?如果我输入我的值 72 体重和 1.80 公斤,它说你是正常体重,是的,这是正确的,但是每当我输入我的值 >25 时,它又说你是正常体重,我哪里做错了?

but whenever i enter my values >25 it's says again you are normal weight但是每当我输入我的值> 25它再次说你是正常体重

Because there's no value greater than 25 which doesn't satisfy this condition:因为没有大于25的值不满足此条件:

} else if (bmi > 18.6) {

It sounds like you just want the first and third conditions, and everything else is "normal":听起来您只想要第一个和第三个条件,其他一切都是“正常的”:

if (bmi < 18.5) {
  console.log("You are underweight")
} else if (bmi > 25) {
  console.log("you are overweigh")
} else {
  console.log("You are normal weight")
}

Each step in the switch is evaluated in order, and the first one that's met will execute.开关中的每一步都按顺序进行评估,第一个遇到的将执行。

In this case, any value above 18.6 will hit your second case.在这种情况下,任何高于 18.6 的值都会影响您的第二种情况。 To get it to pass by yo the third case, you need an upper limit on the value.为了让它通过第三种情况,你需要一个值的上限。

...
else if (bmi>18.6 && bmi <= 25){
...

There is just a small issue in your calculateBmi function.您的 calculateBmi function 中只有一个小问题。

See, whenever Bmi is coming greater than 25, the second condition in your else if ladder gets satisfied and "You are normal weight" gets printed in the console.看,每当 Bmi 大于 25 时,else 中的第二个条件如果梯子得到满足并且“你是正常体重”就会打印在控制台中。

Fix condition as done below, and your issue will be fixed.如下所示修复条件,您的问题将得到修复。

 let calculateWeight = parseFloat(prompt("Enter your weight")) let calculateHeight = parseFloat(prompt("Enter your height?")) function calculateBmi (weight, height){ let bmi = weight / (height * height) if(bmi < 18.5){ console.log("You are underweight") }else if (bmi>18.6 && bmi<=25){ console.log("You are normal weight") }else if (bmi>25){ console.log("you are overweigh") } return bmi; } console.log(calculateBmi(calculateWeight, calculateHeight))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM