gpt4 book ai didi

javascript - JavaScript 中的货币转换器

转载 作者:行者123 更新时间:2023-11-28 04:05:03 26 4
gpt4 key购买 nike

我正在尝试完成此练习:

  1. Ask the user how much money they have.

  2. Next, ask them what currency they have

  3. Finally, ask them what they want to convert it into

  4. Convert the currency to the requested output

  5. Print a nice alert message with the value in the other currency

  6. Your code must use a minimum of two functions.

  7. One function should calculate the conversion.

  8. One function should print the alert message with the converted currency and the currency name.

Hint - It probably makes sense to use two conversion functions. Onefunction to convert from any currency into USD and another to convertfrom USD into anything other currency.

这是我的代码:

当我在浏览器中运行代码时,没有任何反应。如果我正确计算转换并且当我运行此命令时什么也不会打印出来,那么我就不是真的。

我解决了问题,所以它现在正在运行,但我仍然没有得到函数转换部分

'use strict';
let money = Number(prompt("How much money do you have?."));
let moneyCurrency = prompt("What currency are you using?.");
let currencysys = prompt("What currency do you want to convert it to? EUR, JPY, GBP, USD, or BRL");


function convertCurrency(currency, currencySystem) {
if (currencySystem === 'JPY' || currencySystem === 'jpy') {
return convertJPYtoUSD(currency);
}
else if (currencySystem === 'Eur' || currencySystem === 'eur') {
return convertEurtoUSD(currency);
}
else if (currencySystem === 'GBP'|| currencySystem === 'gbp') {
return convertGBPtoUSD(currency);
}
else if ( currencySystem === 'BRL'|| currencySystem === 'brl') {
return convertBRLtoUSD(currency);
}
}

function convertJPYtoUSD(JPY) {
return (JPY * 0.91);
}

function printCurrencyMessage(convertedCurrency, currencysys,round) {
if (round === undefined || isNaN(Number(round))) {
round = 0;
}
}

console.log("The converted currency is "+currencySystem + ".");

我遇到了一些麻烦,数学代码不正确,100GBP 兑换 JPY 应该是 19,103.08,但我得到的结果完全不同

'use strict';
let money = Number(prompt("How much money do you have?."));
let moneyCurrency = prompt("What currency are you using?.");
let currencysys = prompt("What currency do you want to convert it to? EUR, JPY, GBP, USD, or BRL");



let currentExchange = {
"USD": 1,
"EURO": 0.91,
"JPY": 124.17,
"GBP": 0.65,
"BRL": 3.51,
}

let currentExchanges = currentExchange[currency];

function convertCurrency(currencyNeeded, moneyAmount) {
let exchange_value = moneyAmount * exchange_factor
return exchange_value
}

function convertCurrency(currencyNeeded, moneyAmount) {
let exchange_factor = currentExchange[currencyNeeded];
let exchange_value = moneyAmount / exchange_factor
let value = moneyAmount * exchange_factor
console.log("The converted amount is $ " + (exchange_value.toFixed(2)) + "in " + currencyNeeded + ".");

return exchange_value
};

convertCurrency(currencyNeeded, moneyAmount);

最佳答案

TDLR;

您正在尝试警告 undefined variable 。

enter image description here

我建议你让你的函数为你返回你的值。像这样的东西:

console.log("The converted currency is "+ convertToCurrency(moneyCurrency, currencysys + ".");

解释和深入挖掘

您的签名

function convertCurrency(currency, currencySystem) {...

创建一个局部变量“currencySystem”,不能在函数范围之外引用该变量。因此,currencySystem 是该函数的局部变量。

一些通用代码/架构建议:

使变量名称更有意义且一致。

使用所有货币兑换比率创建一个 HashMap /对象,并使用单个函数进行数学转换。 IE:

var currencies = {
usd: {
conversion: 1,
},
eur: {
conversion: .89
},
...
};

function convertCurrency(fromCurrency, toCurrency, amount) {

// ... not the best at thinking of the algorithm. Suggestions?
// perhaps normalize fromCurrency to the dollar, then convert to toCurrency since I believe the dollar is the base/universal currency
};

var convertedCurrency = convertCurrency("USD", "EUR", 20.50);

alert("The converted amount is" + convertedCurrency);

如果您需要多个函数作为要求,那么拆分为每种货币转换并不是一个可怕的主意,但似乎有点太多的开销。

这是最终的工作解决方案:

'use strict';
let amount = Number(prompt("How much money do you have?."));
let currentCurrency = prompt("What currency are you using?");
let desiredCurrency = prompt("What currency do you want to convert it to? EUR, JPY, GBP, USD, or BRL");

var currencyRates = {
"USD": 1,
"EUR": .8,
"JPY": .7,
"XXX": 5
};

function convertCurrency(currentCurrency, desiredCurrency, amount) {
var currentRate = currencyRates[currentCurrency];
var desiredRate = currencyRates[desiredCurrency];

var USDAmount = amount * currentRate;
var convertedAmount = USDAmount / desiredRate;

return convertedAmount; // I think this is the right algorithm :/
}



var convertedCurrencyAmount = convertCurrency(currentCurrency, desiredCurrency, amount);

alert ("Converted: " + convertedCurrencyAmount);

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

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