在我需要总量的时候生产NAN
Producing NAN when i need the total
程序需要为presentValue
、months
和interest
的速率生成一个介于.1%-.10%之间的随机数。
在进行最后的计算时,我得到了NaN。
var count = 5;
function futureValue(presentValue, interest, months) {
var step1 = 1 + Number(interest);
var step2 = parseFloat(Math.pow(step1, months));
var step3 = presentValue * step2;
return "The future value is: " + step3;
}
for (i = 0; i < count; i++) {
var presentValue = Math.floor(Math.random() * 100)
var interest = ((Math.random() * .10 - 0.1) + .1).toFixed(2)
var months = Math.floor(Math.random() * 100)
futureValue(presentValue, interest, months)
console.log("The present value is: " + presentValue);
console.log("The interest rate is: " + interest);
console.log("The number of months is: " + months);
console.log(futureValue());
}
您需要传入参数:
console.log(futureValue())
至
console.log(futureValue(presentValue,interest,months))
这一行正确地计算了未来的值,但对它没有任何作用。
futureValue(presentValue,interest,months);
此行返回调用不带参数的futureValue
函数,该函数返回NaN
并将结果写入日志。
console.log(futureValue());
您应该做的是将值分配给一个变量,然后记录该值:
var futureVal = futureValue(presentValue,interest,months);
console.log(futureVal);
或者只是:
console.log(futureValue(presentValue,interest,months));
您正在调用不带参数的futureValue()。它返回NaN(不是数字),因为您使用"未定义"进行计算,实际上,它不是数字,
尝试:
var count = 5
function futureValue(presentValue,interest,months){
var step1 = 1 + Number(interest);
var step2 = parseFloat(Math.pow(step1,months));
var step3 = presentValue*step2;
return("The future value is: " + step3);
}
for (i=0;i<count;i++){
var presentValue = Math.floor(Math.random()*100)
var interest = ((Math.random()*.10-0.1)+.1).toFixed(2)
var months = Math.floor(Math.random()*100)
var fv = futureValue(presentValue,interest,months) //save your futureValue in a variable.
console.log("The present value is: " + presentValue);
console.log("The interest rate is: " + interest);
console.log("The number of months is: " + months);
console.log(fv)//log your calculated future value
}
这是因为
return("The future value is: " + step3);
是一个字符串。因此,它确实不是一个数字。
您应该返回JUST数字,然后创建字符串。
一旦调用futureValue(presentValue,interest,months)
,该值就会消失。如果你想console.log结果,你应该把它存储在一个变量中,然后console.log。
相关文章:
- JavaScript NaN equvialence
- 为什么react/redux前端项目在package.json中包含生产依赖项
- 如何为排除无穷大和NaN的数字声明Flow类型
- 如何为生产构建angular2应用程序
- 为什么我在这个javaScript代码中使用NaN
- 如何防止Math.max()返回NaN
- 函数将输出nan而不是数字,为什么
- webpack开发模式和生产构建模式之间有什么区别
- jquery中出现NaN错误.已经尝试过ParseInt,但仍然不起作用
- 尝试求和时,在我的表的页脚中返回$NaN
- 我的jquery中出现NaN错误
- Rails 4资产管道Heroku生产javascript参考问题
- 金额结果为NaN
- 使用running total时使用Javascript$NaN
- 使用jquery CounterUp提供NaN的百万值
- 为什么Math.pow(1,无穷大)返回NaN
- parseFloat() returning NaN
- 为什么分配给“NaN”或“未定义”会导致类型错误
- 不是每次NaN显示时都在jquery中进行乘法运算
- 在我需要总量的时候生产NAN