【问题标题】:Understanding JavaScript Mode Function了解 JavaScript 模式函数
【发布时间】:2018-01-21 21:21:47
【问题描述】:

通过贡献,我设法找到了编写函数来查找给定数字集的模式的代码。我试图准确了解该函数的作用,但我被困在该函数的 1 部分。

我尝试在控制台记录所有内容,但我无法理解其中的一部分。

在我们按照出现次数对数组中的数字进行排序后,我们使用另一个循环来获取出现次数最多的数字的键值。在此之前,我们声明变量 compare 并将其设置为 0 和一个空模式变量。

我无法理解的部分是理解为什么我们需要将 freq[item] 与 compare 变量进行比较,如果该语句为真,我们需要将 compare 设置为 freq[item]

有人可以解释一下到底发生了什么吗?

干杯。

function getMostFrequent(arr) {
  var freq = {}
  
  for (item of arr) {
    freq[item] ? freq[item]++ : freq[item] = 1
  }  
  
  var compare = 0
  var mode
  
  for (item in freq) {
    if (freq[item] > compare) {
      compare = freq[item]
      mode = item
    }
  }
  console.log(parseInt(mode))
}

getMostFrequent([1,1,3,3,2,2,5,5,5,3,3,3,3]);

【问题讨论】:

    标签: javascript logic


    【解决方案1】:

    这是你指的部分:

    var compare = 0;
    var mode;
    
    for (item in freq) {
      if (freq[item] > compare) {
        compare = freq[item];
        mode = item;
      }
    }
    

    这会计算所有freq[item]s 中的最大值。将变量 compare 重命名为 maximum 可能会更好,因为这就是它最后的样子。

    if 条件仅适用于大于该最大值的频率 (freq[item]),因此将 compare 设置为更大的频率值。换句话说,compare 是迄今为止所有freq[item]s 中最大的freq[item]

    最后的语句将是mode = item;,它只在最后一个最大频率(即总体最大值)中调用,它将mode 设置为最常见的item

    【讨论】:

    • 如果你能提供一个去掉那行的案例就好了。
    • 是的,我同意@Taurus
    • @Xufox 我只是不明白比较变量如何知道返回该对象键
    • @Taurus 再说一遍,哪一行? “案例”是什么意思?
    • compare = freq[item] 行,例如没有那行,如果你通过[2, 2, 3]mode 将被设置为3
    【解决方案2】:

    请参阅下面的 cmets。希望有帮助。

    function getMostFrequent(arr) {
    
      // freq starts empty
      var freq = {}
    
      // then we start to check every item on arr, our data
      for (item of arr) {
    
        // is there any attribute on freq called item? 
        // ex: freq[2] is there?
        // if yes, increment it.
        // if not, set it to 1, it's the first time we met him.
        freq[item] ? freq[item]++ : freq[item] = 1
      }  
    
      // compare starts at the lowest value,
      // assuming only positive numbers. 
      var compare = 0
    
      // we don't know the mode yet. keep it undefined.
      var mode
    
      // now we loop every attribute present in freq. 
      for (item in freq) {
    
        // have we counted freq[item] more times 
        // than the actual value  of compare? 
        if (freq[item] > compare) {
          // if yes, we store that number on compare
          compare = freq[item]
          // also, item (which is a number too!) is, as far as we know,
          // our mode. 
          mode = item
        }
    
        // loop shall repeat untill we check every atribute inside freq 
      }
    
      // and finally we print mode, since it must the the mode
      console.log(parseInt(mode))
    }
    
    // call the function passing sample data
    getMostFrequent([1,1,3,3,2,2,5,5,5,3,3,3,3]);
    

    【讨论】:

      猜你喜欢
      • 2011-11-11
      • 2012-07-22
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 2019-11-07
      • 1970-01-01
      相关资源
      最近更新 更多