After reviewing the Mini Linter solution under Get Unstuck, I wasn’t too crazy about having to hard-code the individual overused words inside conditional statements. Besides, I certainly don’t want as many conditional statements as there are words in the overusedWords array.
Here’s a general solution that capitalizes on the various lessons and the recommended Mozilla documentation.
// Count total number of occurrences of overused words.
// Initialize count as an empty array.
let overusedWordCount = ;
storyWords.forEach(word => {
for (i = 0; i < overusedWords.length; i++) {
if (isNaN(overusedWordCount[i])) {
overusedWordCount.push(0);
}
if (word === overusedWords[i]) {
overusedWordCount[i]++;
}
}
})
// Display total number of occurrences of overused words.
overusedWords.forEach(word => {
const i = overusedWords.indexOf(word);
console.log(word + ': ’ + overusedWordCount[i]);
})
I know you posted this a few months ago, but thank you so much! I, too, was bothered by the multiple conditionals in the solution and KNEW that there must be a way to generalize it. I struggled for a few hours trying to come up with a better solution, but couldn’t get mine to work. Your solution helped me realize what, exactly, I wanted to do with my code and then I was able to fix my errors. Thank you again!
I used the filter method to avoid hard-coding. The filter method creates the new array howManyOverusedWords, then I console logged the length of this array.
const howManyOverusedWords = storyWords.filter( word => {
if (overusedWords.includes(word) === true){
return word
}
}
)
console.log(howManyOverusedWords.length)
I think this worked, I made an array filled with each instance of the overused words in the story, then I counted the length of that array.
The concise body arrow function has an implicit return when the body is a simple expression.
That is a subsequent code block (meaning after the filter). It will throw an error, return outside a function or some such. Everything we need is right in the filter expression.
array.method(param => otherArray.method(arg))
It helps to break down what the filter callback is doing…
w is the iteration variable
.filter() has a built in iteration loop that goes through array a, with w being one element at a time. Each element is passed to the .includes() method on array b. If found, that value is passed up to the filter to append to the return array.
This seems to identify how to list the words that we want to remove. I don’t understand how you go from that to removing the words included. I tried the following but it didn’t work…
const betterWords = storyWords.filter (word => word != unnecessaryWords.includes (word));
I have been trying to complete the extra tasks with some difficulty. I believe I found a way to write a function that find the word that appears the greatest number of times but I don’t understand how it works. Any help on how to understand it would be greatly appreciated! This is the code I have:
function mostFrequent(arr) {
arr.sort();
let maxCount = 1, res = arr[0];
let currentCount = 1;
for (let i = 1; i < arr.length; i++) {
if (arr[i] === arr[i - 1])
currentCount++;
else {
if (currentCount > maxCount) {
maxCount = currentCount;
res = arr[i - 1];
} currentCount = 1
}
} if (currentCount > maxCount)
{
maxCount = currentCount;
res = arr[n - 1];
}
return res;
}
console.log(mostFrequent(storyWords))
mostFrequent(storyWords);
let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey. The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';
let overusedWords = ['really', 'very', 'basically'];
let unnecessaryWords = ['extremely', 'literally', 'actually' ];
let storyWords = story.split(' ');
const betterWords = storyWords.filter(word => word !== unnecessaryWords[0] && word !== unnecessaryWords[1] && word !== unnecessaryWords[2]);
let reallyCount = 0;
let veryCount = 0;
let basicallyCount = 0;
let overusedWordCount = 0;
for (word of storyWords) {
if (word === 'really') {
reallyCount += 1;
} else if (word === 'very') {
veryCount += 1;
} else if (word === 'basically') {
basicallyCount += 1;
} overusedWordCount = reallyCount + veryCount + basicallyCount;
}
let sentenceCounter = 0;
for (word of storyWords) {
if (word.includes('.') || word.includes('!')) {
sentenceCounter += 1;
}
}
console.log(storyWords.length);
console.log(sentenceCounter);
console.log(overusedWordCount);
const logInfo = (storyWords, sentenceCounter,overusedWordCount) => {
console.log('Word count: ' + storyWords.length + '. Sentence count: ' + sentenceCounter + '. Overused word appearances: ' + overusedWordCount + '.')
}
logInfo(storyWords, sentenceCounter, overusedWordCount)
//console.log(betterWords.join(' '));
function mostFrequent(arr) {
arr.sort();
let maxCount = 1, res = arr[0];
let currentCount = 1;
for (let i = 1; i < arr.length; i++) {
if (arr[i] === arr[i - 1])
currentCount++;
else {
if (currentCount > maxCount) {
maxCount = currentCount;
res = arr[i - 1];
} currentCount = 1
}
} if (currentCount > maxCount)
{
maxCount = currentCount;
res = arr[n - 1];
}
return res;
}
console.log(mostFrequent(storyWords))
mostFrequent(storyWords);
I agree. They limited the program to those only words. I wrote it so that the contents of overusedWords and unnecessayWords arrays could change and the program would still work. I am surprise their solution did not.
Like so many before me, I too was bothered by this. Not only the conditionals, but all the variables. What if the overusedWords array has more than 3 elements? Who is going to sit and hard code 1000 variables and 1000 conditionals? I was trying to resolve it with a .forEach on the overusedWords array and a .reduce to count the number of uses, but I couldn’t get it to work. Thank you for posting your solution!
EDIT: Peterghostine’s solution didn’t work. It kept throwing up errors, and I eventually gave up working my way through them. I went back to my original approach but with nested for loops. I’ve tested this, and it works:
let uses = 0;
let currentUse = 0;
let currentWord;
for (let i = 0; i < overusedWords.length; i += 1) {
currentWord = overusedWords[i];
for (let j = 0; j < betterWords.length; j += 1) {
if (overusedWords[i] === betterWords[j]) {
currentUse += 1;
}
uses += currentUse;
currentUse = 0;
}
console.log(You used the word ${currentWord} ${uses} times.);
uses = 0;
}