在编码练习中,我需要构建一个加密货币转换器。
为了练习,它应该从一个将数据传递给其他函数的函数开始。仅应调用第一个函数以级联到相应的结果。
- addCurrency(将采用3个参数并检查货币是否在名为 crypt 的数据库中)
- findcurrency(将从第一个函数中获取基本值并将其传递给下一个函数)
- 转换器(将进行实际转换
- tellConversion(“您将收到 2 个 {COINS_NAME} 的 {AMOUNT} 美元”)
addCurrency 接受 3 个参数:一枚硬币、其值(value)(硬币数量)和一组硬币(硬币数据库),因此示例调用如下所示
addCurrency({coin:'bitcoin', rate:8000}, 2, crypt)
如果硬币不在数据库中,则应返回:
"New coin {YOUR_NEW_ADDED_COIN_GOES_HERE} added to Database"
但是硬币名称大写!!!并将硬币存储在地窖中。
如果硬币在数据库中,它应该返回
"You will receive {AMOUNT} usd for your 2 {COINS_NAME}"
这是我的代码。有两个问题:不确定如何大写以及转换器无法工作(不确定原因)。
Anyone who can help me?
var crypt = []; // define our database
var addCurrency = function (coin, value, crypt) {
debugger;
var indexObject = null; //helper variable to check coin in database
//does the coin exist
for (var i = 0; i < crypt.length; i++) { //loop through the crypt in order to ...
var obj = crypt[i].coin; //... set a variable with the current iterations coin name
if (obj == coin.coin) {//if that object happens to be the passed in coins'name ...
indexObject = obj; //... set the value of indexObject to that value (want to store it for futre cod)
}
}
//if coin not found, create a new one
if(indexObject == null) { //... if the helper stays null ...
//coin.coin = coin.coin.charAt(0).toUpperCase() + coin.coin.slice(1) //capitalize crypto currency
indexObject = { //set index object with ...
"coin": coin.coin, "rate": coin.rate //...this object patter
}
crypt.push(indexObject);
return `New coin ${coin.coin} added to Database`
//here passing between variable start. conversion rate is passed to findcurrency function
} else {
var passedOn = coin.rate
var value = value
findcurrency(passedOn, value)
}
}
function findcurrency(passedOn, value) {
converter(passedOn, value)
}
function converter(passedOn, value) {
var converted = passedOn * value
tellConversion(converted, value)
}
function tellConversion(converted, value) {
return `You will receive ${converted} usd for your 2 ${value}`
}
最佳答案
您不会从 addCurrency、findcurrency 和转换器的 else 分支返回。
在最后一个语句之前添加“return”,它应该可以工作。
例如:
var crypt = []; // define our database
var addCurrency = function (coin, value, crypt) {
debugger;
var indexObject = null; //helper variable to check coin in database
//does the coin exist
for (var i = 0; i < crypt.length; i++) { //loop through the crypt in order to ...
var obj = crypt[i].coin; //... set a variable with the current iterations coin name
if (obj == coin.coin) {//if that object happens to be the passed in coins'name ...
indexObject = obj; //... set the value of indexObject to that value (want to store it for futre cod)
}
}
//if coin not found, create a new one
if(indexObject == null) { //... if the helper stays null ...
//coin.coin = coin.coin.charAt(0).toUpperCase() + coin.coin.slice(1) //capitalize crypto currency
indexObject = { //set index object with ...
"coin": coin.coin, "rate": coin.rate //...this object patter
}
crypt.push(indexObject);
return `New coin ${coin.coin} added to Database`
//here passing between variable start. conversion rate is passed to findcurrency function
} else {
var passedOn = coin.rate
var value = value
return findcurrency(passedOn, value)
}
}
function findcurrency(passedOn, value) {
return converter(passedOn, value)
}
function converter(passedOn, value) {
var converted = passedOn * value
return tellConversion(converted, value)
}
function tellConversion(converted, value) {
return `You will receive ${converted} usd for your 2 ${value}`
}
关于javascript - 带有对象、数组的转换器通过函数传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57934502/