gpt4 book ai didi

javascript - Nuxt.js 代码在从数组计算数据时给出 NaN 错误

转载 作者:行者123 更新时间:2023-11-30 19:43:04 28 4
gpt4 key购买 nike

我有用于计算在 nuxt envoriment 中工作的给定数组的订单的代码。我创建了 3 个函数,每个函数都从数组中计算一个数字,如果我使用 console.log 第一个函数,它会给我一个真实的结果,但连接到它的其他函数的计算方式类似,但是当我将它们放入 console.log 时,它们会给出 Nan错误。(数组以字符串格式出现)

我已经在每个可能的地方尝试了 parseFloat() 但控制台再次出现 Not a Number 错误

const express = require('express')
const axios = require('axios')
const consola = require('consola')
const { Nuxt, Builder } = require('nuxt')
const app = express()

// Import and Set Nuxt.js options
const config = require('../nuxt.config.js')
config.dev = !(process.env.NODE_ENV === 'production')

let loopInterval = null

async function start() {
// Init Nuxt.js
const nuxt = new Nuxt(config)

const { host, port } = nuxt.options.server

// Build only in dev mode
if (config.dev) {
const builder = new Builder(nuxt)
await builder.build()
} else {
await nuxt.ready()
}

// Add this part
app.get('/start', (req, res) => {
codeLoop()
res.send('My code loop started.')
})

app.get('/stop', (req, res) => {
clearInterval(loopInterval)
loopInterval = null
res.send('My code loop stopped')
})

// Give nuxt middleware to express
app.use(nuxt.render)

// Listen the server
app.listen(port, host)
consola.ready({
message: `Server listening on http://${host}:${port}`,
badge: true
})
}
start()
let UsdtTrySell = [["5.45","400"],["5.44","400"],["5.43","400"]];
let BtcUsdtSell = [["3900","1"],["3905","1"],["3910","1"]];
let btcTryBuy = [["21000","1"],["20950","1"],["20900","1"]];
let UsdtOrder = 1000;

function runMyCode() {
consola.log('hello')
}
function codeLoop() {
runMyCode()
loopInterval = setInterval(async () => {
function SellUsdtTryAtBtrk() {
let inputOrder1 = UsdtOrder
let sum = 0
let i = 0
for (i = 0; i < parseFloat(UsdtTrySell.length); i++) {
if (parseFloat(UsdtTrySell[i][1]) < inputOrder1) {
sum += UsdtTrySell[i][0] * UsdtTrySell[i][1]
inputOrder1 -= parseFloat(UsdtTrySell[i][1])
} else {
return sum + inputOrder1 * parseFloat(UsdtTrySell[i][0])
}
}
return ''
}
function BuyBtcTryAtBtrk() {
let inputOrder2 = SellUsdtTryAtBtrk
let sum1 = 0
let i = 0
for (i = 0; i < (parseFloat(btcTryBuy.length)); i++) {
if ( parseFloat(btcTryBuy[i][0]) * parseFloat(btcTryBuy[i][1]) <
parseFloat(inputOrder2)) {
sum1 += parseFloat(btcTryBuy[i][1])
inputOrder2 -= parseFloat(btcTryBuy[i][0]) *
parseFloat(btcTryBuy[i][1])
} else {
return sum1 + parseFloat(inputOrder2) /
parseFloat(btcTryBuy[i][0])
}
}
return ''
}
function SellBtcUsdtAtBnnc() {
let inputOrder3 = BuyBtcTryAtBtrk
// const BtcUsdtSell = bnncBtcusdt.bids
let sum = 0
let i = 0
for (i = 0; i < (parseFloat(BtcUsdtSell.length)); i++) {
if (parseFloat(BtcUsdtSell[i][1]) < inputOrder3) {
sum += BtcUsdtSell[i][0] * BtcUsdtSell[i][1]
inputOrder3 -= parseFloat(BtcUsdtSell[i][1])
} else {
return sum + inputOrder3 * parseFloat(BtcUsdtSell[i][0])
}
}
return ''
}
console.log(SellBtcUsdtAtBnnc());
}, 2 * 1000) // Runs every 2 seconds
}

最佳答案

您正在将 inputOrder2inputOrder3 分配给函数,而不是将它们分配给调用函数的结果 .


改变这一行:

let inputOrder2 = SellUsdtTryAtBtrk

...为此:

let inputOrder2 = SellUsdtTryAtBtrk();

...并更改此行:

let inputOrder3 = BuyBtcTryAtBtrk

...为此:

let inputOrder3 = BuyBtcTryAtBtrk();

...它应该可以工作。

关于javascript - Nuxt.js 代码在从数组计算数据时给出 NaN 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55202830/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com