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]
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.
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.
The overall statement is an assignment of the current property value plus 1 back onto the property.
a = a + 1
If the property exists, this will happen,
frequency[word] = frequency[word] + 1
and if it does not exist, we create it, set it to 0, then add 1.
We know that the act of making an assignment to a dictionary is as simple as,
frequency[word] = 0 // establish the property and initialize
frequency[word] += 1 // up the value by 1
We would not be creating this property if we didn’t have a word that was just encountered for the first time. So it must have a value of 1, to start. But because we are doing the math in one expression, we use the logic so we don’t have to write a bulky if statement.
Now consider how OR works when the operands are truth values., not booleans.
A = ''
B = 'b'
A || B => 'b'
A = 'a'
A || B => 'a'
I’m sure I cannot take credit for creating that logic, but must credit whomever I learned the logic from. Now it’s just a matter of paying it forward. We stand on the shoulders of those who come before us.
I started learning web development from scratch almost three months ago and I really appreciate that people like you with years of experience, share their knowledge and help us begginers to learn this beautiful skill
Sorry for bumping an old post but I’ve been trying to do the extra assignments on the Mini Linter project as well and while I understand most of what this code does, I cannot understand why ‘frequency’ was made an object.
@mtf could you please clarify a bit more on this? Thank you for your time.
Frequency is an object so it can be made up as a table, of sorts. Each word is a key, and the word count is the assigned value.
When we first encounter a word, it is set as a key with with initial value, zero, then incremented. Each new encounter of the same word increments the count, again.