我想要有关为什么toFixed附加到用户输入var usergb时在我的JS警报中不起作用的反馈。是一件奇怪的事情。我被鼓励发表,并且在这里我没有看到关于用户输入变量的toFixed问题的其他任何问题。
var downloadspeed = function () {
var usergb = prompt("How many gigabytes do you wish to download at 56 kilobits per second?");
console.log(usergb);
var hours = (usergb*8388608)/201600;
console.log(hours);
var days = (usergb*8388608)/4838400;
console.log(days);
//below in the alert is where you will see it, after the first concatenated string, I'd like to type usergb.toFixed(2) but it won't take.
alert("The number of hours it will take to download a " + usergb + " GB file at 56 kilobits per second is " + hours.toFixed(2) + " hours. This might be a big number. So, in terms of days to download, it'll take " + days.toFixed(2) + " days to download. This is why we have faster broadband speeds nowadays and why we have video streaming.");
}
downloadspeed();
我得到的错误是它不是一个函数。
卢(Grazie)
最佳答案
您需要将输入强制转换为数字:
var usergb = +prompt("How many gigabytes do you wish to download at 56 kilobits per second?");