尽管我将切片从数字转换为字符串,但它给了我一个错误,即切片不起作用。 问题出在哪里?
var weight = prompt("Enter your Weight");
var height = prompt("Enter your Height");
function bmi(weight, height) {
var result = weight / Math.pow(height, 2);
var count = result.toString.slice(0, 4);
if (result < 18.5) {
alert("Your result is " + count + ", so you are underweight.");
} else if (result >= 18.5 && result <= 24.9) {
alert("Your result is " + count + ", so you have a normal weight.");
} else {
alert("Your result is " + count + ", so you are overweight.");
}
}
bmi(weight, height);
我将切片从数字转换为字符串,但给了我同样的错误,问题出在哪里?
你实际上并没有打电话给
toString()
。
toString
解析为函数对象,然后您可以使用 ()
调用该函数对象以获取字符串。您看到的错误告诉您函数对象本身没有 slice()
方法,这是正确的。