当我需要总数时生成NAN

时间:2015-12-02 21:04:23

标签: javascript nan

该计划需要生成presentValuemonthsinterest费率的随机数,介于.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());
}

5 个答案:

答案 0 :(得分:5)

您需要传递参数:

console.log(futureValue())

console.log(futureValue(presentValue,interest,months))  

答案 1 :(得分:2)

您正在调用没有参数的futureValue()。它返回NaN(非数字),因为您正在使用&#34; undefined&#34;进行计算。确实,这不是一个数字,

尝试:

&#13;
&#13;
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

}
&#13;
&#13;
&#13;

答案 2 :(得分:2)

此行正确计算未来值,并且不执行任何操作。

package main

import (
    "log"

    "github.com/gin-gonic/gin"
    "github.com/jinzhu/gorm"
    _ "github.com/mattn/go-sqlite3"
)

// ApiMiddleware will add the db connection to the context
func ApiMiddleware(db gorm.DB) gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Set("databaseConn", db)
        c.Next()
    }
}

func main() {
    r := gin.New()

    // In this example, I'll open the db connection here...
    // In your code you would probably do it somewhere else
    db, err := gorm.Open("sqlite3", "./example.db")
    if err != nil {
        log.Fatal(err)
    }

    r.Use(ApiMiddleware(db))

    r.GET("/api", func(c *gin.Context) {
        // Don't forget type assertion when getting the connection from context.
        dbConn, ok := c.MustGet("databaseConn").(gorm.DB)
        if !ok {
            // handle error here...
        }

        // do your thing here...
    })

    r.Run(":8080")
}

此行返回调用没有参数的futureValue(presentValue,interest,months); 函数,返回futureValue并将结果写入日志。

NaN

您应该做的是将值赋给变量,然后记录该值:

console.log(futureValue());

或者只是:

var futureVal = futureValue(presentValue,interest,months);
console.log(futureVal);

答案 3 :(得分:0)

那是因为

with open("users") as input_file:
    for line in input_file:
        username, password = line.split('|')
        print("Username:", username.strip())
        print("Password:", password.strip())

是一个字符串。所以,它确实不是一个数字。

你应该返回JUST数字,然后创建字符串。

答案 4 :(得分:0)

拨打futureValue(presentValue,interest,months)后,该值就会消失。如果你想在console.log中得到结果,你应该将它存储在变量中,然后将它存储在console.log中。

相关问题