【问题标题】:JavaScript passing variables between functions to target an object in an arrayJavaScript 在函数之间传递变量以定位数组中的对象
【发布时间】:2021-03-04 04:11:03
【问题描述】:

(警告)第一次发帖,尽我所能。如果格式错误,请提前道歉。

我正在按照以下说明创建一个简单的加密货币转换器:

*定义一个名为 'addCurrency' 的函数,它接受 3 个参数:一个 'coin' 一个 'value' 和一个数组 ('crypt')

内部使用 Array.prototype.findIndex 来查看 'coin' 是否已经存在于 'crypt' 数组中。如果找到索引,则存储它。

如果不是: 将其推送到“crypt”数组。 然后返回一个字符串,如下所示,将单词 COIN 替换为您添加的“硬币”的名称(大写)。

如果有,请调用“findCurrency”以获取汇率。

定义一个名为 'findCurrency' 的函数,它接受 3 个参数:一个 'coin' 一个 'value' 一个索引(来自 'addCurrency')和 'crypt' 数组。 使用索引从 'crypt' 数组中访问正确的 'coin' 来获取 'coin' 的汇率。

调用传递“价值”、汇率和“硬币”的转换器函数。 定义一个名为 converter 的函数,它接受 3 个参数,一个“价值”、一个汇率和一个“硬币”。

执行转换并返回调用tellConversion以输出结果。 定义一个名为 tellConversion 的函数,它接受 3 个参数作为结果,一个“硬币”和一个“值”。

使用tellConversion的参数创建一个字符串,如示例所示。*

在控制台中运行下面的代码,我得到:

未捕获的类型错误:无法读取未定义的属性“速率”

在这一行 --> var coinRate = crypt[coinIndex].rate;

coinIndex 注销到控制台,并在初始函数调用中传递 crypt。为什么 var coinRate 未定义?

我错过了什么?

var addCurrency = (coinObj, value, crypt) => {
    
    function findCurrency(crypt, coinIndex, value){
-->     var coinRate = crypt[coinIndex].rate;
        console.log(coinRate)
        converter(value, coinRate)
    }
            
    function converter(value, coinRate){
      var converted = coinRate * value;
        console.log(converted)
        tellConversion(value, converted, coinObj)
    }
    function tellConversion(value, converted, coinObj){
      return `You will receive ${converted} usd for your ${value} ${coinObj.coin}`
    }

    var coinIndex = crypt.findIndex(obj => obj.coin === coinObj.coin)
        console.log(coinIndex)
        if(coinIndex === -1){
            crypt.push(coinObj)
            console.log(crypt)
            return `New coin ${coinObj.coin.charAt(0).toUpperCase() + coinObj.coin.slice(1)} added to Database`
            
        } else { findCurrency(value, coinIndex, coinObj) }
        
  }

调用函数:

var crypt = [{coin:'eth', rate:800}]
addCurrency({coin:'eth', rate:800}, 2, crypt)

【问题讨论】:

  • 我没有查看所有内容,但是当您从转换器调用 tellConversion 时,返回值会丢失,因为转换器不会返回任何内容,如果这有意义的话。

标签: javascript arrays nested javascript-objects


【解决方案1】:

findCurrency 函数的参数传递顺序不正确。

更新 findCurrency 函数以仅接受 coinIndex 作为参数。

function findCurrency(coinIndex){
  var coinRate = crypt[coinIndex].rate;
  console.log(coinRate)
  converter(value, coinRate)
}

valuecrypt 变量将在 findCurrency 中可用,因为它们在同一范围内。

总体代码:

var addCurrency = (coinObj, value, crypt) => {
    
  function findCurrency(coinIndex){

     var coinRate = crypt[coinIndex].rate;
      console.log(coinRate)
      converter(value, coinRate)
  }
          
  function converter(value, coinRate){
    var converted = coinRate * value;
      console.log(converted)
      tellConversion(value, converted, coinObj)
  }
  function tellConversion(value, converted, coinObj){
    return `You will receive ${converted} usd for your ${value} ${coinObj.coin}`
  }

  var coinIndex = crypt.findIndex(obj => obj.coin === coinObj.coin)
      console.log(coinIndex)
      if(coinIndex === -1){
          crypt.push(coinObj)
          console.log(crypt)
          return `New coin ${coinObj.coin.charAt(0).toUpperCase() + coinObj.coin.slice(1)} added to Database`
          
      } else { findCurrency(coinIndex) }
      
}

var crypt = [{coin:'eth', rate:800}]
addCurrency({coin:'eth', rate:800}, 2, crypt)

【讨论】:

  • 我做了上面的更改,现在它一直工作到var coinRate = crypt[coinIndex].rate; console.log(coinRate),但由于某种原因没有执行函数调用:converter(value, coinRate)
猜你喜欢
  • 2011-06-03
  • 1970-01-01
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 2013-04-09
  • 2013-03-07
  • 1970-01-01
相关资源
最近更新 更多