简体   繁体   中英

Understanding JavaScript Mode Function

Through contributions, I've managed to find code that writes a function to find the mode of a given set of numbers. I'm trying to understand exactly what the function does but I am stuck in 1 section of the function.

I tried console logging everything but I couldn't understand 1 part.

After we sort the numbers in an array by their appearances, we use another loop to get the key value of the number that appears the most. Before that, we declare the variables compare and set it to 0 and an empty mode variable.

The part I can't grasp my head around is understanding why we need to compare freq[item] to the compare variable and if that statement is true, we need to set compare to freq[item] .

Can someone be as kind to explain exactly what is going on there?

Cheers.

 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]); 

This is the part you're referring to:

var compare = 0;
var mode;

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

This computes the maximum among all freq[item] s. It's probably better to rename the variable compare to maximum , since that's what it's going to be at the end.

The if condition is only true for frequencies ( freq[item] ) that are larger than that maximum, so compare is being set to that bigger frequency value. In other words, compare is the maximum freq[item] of all freq[item] s seen so far.

The final statement will then be mode = item; which is only called for the last maximum frequency (which is the maximum overall), which sets the mode to the most common item .

see the comments bellow. hope it helps.

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM