我正在做一个BMI计算器的技术评估,但我被困在使用公式的阶段。计算BMI的说明如下:
指令1: 每个用户的高度都是以英尺表示的,因此计算首先需要将其转换为米。提示:多重高度由0.3048. 指令2: BMEye用一种先进的算法计算体重指数!BMEye有最健康饮食的国家的概念,他们是乍得,塞拉利昂,马里,冈比亚,乌干达,加纳,塞内加尔,索马里,象牙海岸和以色列。如果用户来自这些国家中的任何一个,那么计算出的BMI值是0.82的倍数,稍微降低了一点。 最后指令: 按照上面的指南和提示,让computeBMI使用用户的体重、身高和国家来计算和返回用户的BMI值。
我想做的事:
我试图在我的本地编辑崇高文本中运行代码,它正确地计算了BMI,但是当我将代码带到运行评估平台的Google分级跟踪器上时,它会抛出一个错误:“您的BMI计算不正确!”
有人能帮我解决这个错误吗?下面是一个函数,它通过抓取高度、重量和国家来帮助计算来接收用户的对象。
const computeBMI = ({weight, height, country}) => {
const LowBMIcountries = ["Chad", "Sierra Leone", "Mali", "Gambia",
"Uganda", "Ghana", "Senegal", "Somalia", "Ivory Coast", "Israel"];
const bmiRate = 0.82;
let ConvertHeight = height * 0.3048;
let BMI = weight / Math.pow(ConvertHeight,2);
if (LowBMIcountries.includes(country)) {
BMI *= bmiRate;
}
return Math.round(BMI, 2);
};发布于 2019-06-06 12:16:14
过了几天,我对这个有挑战性的问题给出了正确的答案,我知道这对将来的其他人是有用的。
const computeBMI = ({weight, height, country}) => {
const countries = ["Chad", "Sierra Leone", "Mali", "Gambia", "Uganda",
"Ghana", "Senegal", "Somalia", "Ivory Coast", "Isreal"];
const hm = height * 0.3048;
const ratio = (countries.includes(country) ? 0.82 : 1);
const BMI = (weight / (hm * hm)) * ratio;
return parseFloat(BMI).toFixed(1);
}; 发布于 2019-06-04 11:26:21
这个问题没有提到四舍五入,更不用说你对Math.round的使用是不正确的。Math.round接受一个参数并将其舍入最近的整数。如果您想要两个小数点,请使用toFixed(2)。
发布于 2019-06-08 14:23:42
研究以下内容..。
entervar computeBMI = function({weight,height,country}) {
const bmiScale = 0.82;
let feetToMeter = 0.3048;
const countries = ['Chad','Sierra Leone','Mali','Gambia','Uganda',
'Ghana','Senegal','Somalia','Ivory Coast','Isreal'];
const heightinmeter = (height * feetToMeter);
let bim = ((weight)/(heightinmeter ** 2));
if(countries.includes(country)) {bim = bim * bmiScale}
var fixednum = bim.toFixed(1);
return fixednum;
} https://stackoverflow.com/questions/56442888
复制相似问题