Any more concise way for this? from Mini Linter project's extras


This is from Codecademy’s Mini Linter project’s extra challenge part. So I can’t find solutions to compare. It would be very helpful if someone tells me whether there is a more concise way to write the code below. The goal is to write a code to find the word that has been used the greatest of times in a text.

Thanks a lot. :v:t3:

// array to .push() counts of words
const counts = [];

//loop to create an array of words and their usages
for (word of betterWords) {
  let times = 0;
  for (element of betterWords) {
    if (element === word) {
      times +=1
    } else {
      times === times
  counts.push ([times, word])

// find the word used the greatest times
const isItThat = [];
for (let a=0; a<counts.length; a++){

console.log(Math.max.apply(null, isItThat))//11



let frequency = {};  
let max = 0;  
let result;   

for (let word of betterWords) {
  word = word.toLowerCase().replace('"', '');
  frequency[word] = (frequency[word] || 0) + 1;
  if (frequency[word] > max) {  
    max = frequency[word];  
    result = word;          

console.log(`The word that appears most is: '${result}', used ${max} times.`);

// The word that appears most is: 'the'; used 14 times.


Hi @mtf,

I learned “objects” today. Didn’t see “advanced objects” lesson yet. I have been staring at this code for couple minutes. I just wanted to say that when I comprehended how you’ve used frequency object there, it made me smile. Maybe it is a very common way to use it, but I think coding is really an art! It is soo logical.
Thanks for giving time for me.

A question , I don’t know how to use .replace() method. I quickly went over the related page on MDN website, but it is a short information. I don’t understand actually. Could you please tell me if there is another source regarding this method where I can learn about it? And, I don’t know RegEx more than a bit, if it is a RegEx method, I will just wait to learn it until I finish the JavaScript path on Codecademy.

Thanks in advance.


String.prototype.replace(target, substitution) will take either a string or a regular expression argument. It is a global method (what I call greedy) and replaces all occurrences of the string or pattern with the string we give to substitute.