我正在尝试进行货币换算。
我建立了一个数据集:
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/