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

#1

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.

``````// 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++){
isItThat.push(counts[a][0])
}

console.log(Math.max.apply(null, isItThat))//11
console.log(isItThat.indexOf(11))//5
console.log(counts[5])//[11,the]
``````

#2

Consider…

``````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.
``````

#3

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.

`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.