Link to home
Start Free TrialLog in
Avatar of Temir Aliev
Temir AlievFlag for United States of America

asked on

On line 35, why did we assign var to a parameter of a function?

function findMaxRepeatCountInWord(word) {
  
  // Break up individual words into individual letters.
  var a = word.split('');
  
  // Count the instances of each letter
  var counted = a.reduce(function(allChars,char){
    if (char in allChars) {
      allChars[char]++;
    } else {
      allChars[char] = 1;
    }
    return allChars;
  }, {});
  
  // Iterate all the counts and find the highest
  // Return this word's max repeat count
  var totals = [];
  for (var key in counted){
    totals.push(counted[key]);
  }
  return totals.reduce(function(x,y) {
    return (x > y) ? x : y;
  });
}


function findFirstWordWithMostRepeatedChars(text) {
  var maxRepeatCountOverall = 0;
  var wordWithMaxRepeatCount = '';
  text.split(" ").forEach(function(word) {
    var repeatCountForWord = findMaxRepeatCountInWord(word);
    if (repeatCountForWord > maxRepeatCountOverall) {
      maxRepeatCountOverall = repeatCountForWord;
      wordWithMaxRepeatCount = word;
    }
  });
    
      
  return wordWithMaxRepeatCount;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
question is fully answered...