【问题标题】:Converter with objects, arrays passed variable through functions带对象的转换器,数组通过函数传递变量
【发布时间】:2020-01-15 23:19:28
【问题描述】:

在编码练习中,我需要构建一个加密货币转换器。

为了练习起见,它应该从一个将数据传递给其他函数的函数开始。只有第一个函数应该被调用级联到相应的结果。

  1. addCurrency(将采用 3 个参数并检查是否存在名为 crypt 的数据库中的货币)
  2. findcurrency(将从第一个函数中获取基本值并将它们传递给下一个函数)
  3. converter(将进行实际转换
  4. tellConversion(“您的 2 个 {COINS_NAME} 将收到 {AMOUNT} 美元”)

addCurrency 接受 3 个参数:硬币、其价值(硬币数量)和硬币数组(硬币数据库),因此示例调用可能如下所示

addCurrency({coin:'bitcoin', rate:8000}, 2, crypt)

如果硬币不在数据库中,它应该返回:

“新硬币 {YOUR_NEW_ADDED_COIN_GOES_HERE} 添加到数据库”

但硬币名称大写!!!并将硬币存放在地穴中。

如果硬币在数据库中,它应该返回

“您的 2 个 {COINS_NAME} 将收到 {AMOUNT} 美元”

这是我的代码。 有两个问题:不确定如何大写和转换器不起作用(不确定原因)。

谁能帮帮我?

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}`
}

【问题讨论】:

  • 如果您想帮助我,请不要大幅更改上面的代码,而是尽量保持我的(初学者)逻辑尽可能多。我敢肯定有更好的方法来解决这个问题
  • 第一个问题是 String.toUpperCase() 方法。对于您的第二个问题,示例输入会有很长的路要走;)
  • array.push(indexObject);我认为你不是故意的。
  • @OP,因为 'crypt' 是在函数中定义的,所以当你调用函数时它总是一个空数组。也许您想将其移出。
  • 即crypt.push(indexObject);

标签: javascript arrays function loops object


【解决方案1】:

您不会从 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}`
}

【讨论】:

  • Awesome Andre,你认为你可以将字符串整合为大写吗?我试过了,但我把它弄混了。
  • 知道了......它必须在返回函数中:return New coin ${coin.coin.charAt(0).toUpperCase()+coin.coin.slice(1)} added to Database
猜你喜欢
  • 1970-01-01
  • 2013-04-14
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多