gpt4 book ai didi

javascript - javascript中的简单货币转换

转载 作者:行者123 更新时间:2023-11-28 16:51:15 24 4
gpt4 key购买 nike

我正在尝试进行货币兑换。

我建立了一个数据集:

const currencyRates = { USD:1.102539, EUR:1, GBP:0.84329691, AUD:1.61545337,JPY:120.4893208, CAD:1.44956315, CHF:1.07099756 }

这是我到目前为止的功能..但它仍然无法正常工作:/

 function currencyConversion (from, to, val) {
return ~~( (+val) * (currencyRates[to] + (1 - currencyRates[from])) )
}

我创建了一组测试


function test(from, to, val, result){
const output = currencyConversion(from, to, val)
return output === result ? ["✔",from, to] : ["✘",from, to,output,result]
}

console.log(test("USD","AUD",1000,1465))
console.log(test("EUR","USD",1000,1102))
console.log(test("EUR","EUR",1000,1000))
console.log(test("EUR","GBP",1000,0843))
console.log(test("EUR","AUD",1000,1615))
console.log(test("USD","EUR",1000,907))
console.log(test("USD","USD",1000,1000))
console.log(test("JPY","USD",1000,9))
console.log(test("GBP","EUR",1000,1185))
console.log(test("AUD","EUR",1000,619))
console.log(test("USD","GBP",1000,764))
console.log(test("GBP","AUD",1000,1915))
console.log(test("AUD","USD",1000,682))
console.log(test("AUD","JPY",1000,74579))

最佳答案

试试这个:

function currencyConversion(from, to, val) {
return ~~(val / currencyRates[from] * currencyRates[to]);
}

关于javascript - javascript中的简单货币转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59917583/

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