I tried really hard to just use what we’ve learned so far, and I found solutions! Overly complicated, that’s for sure, but it helped me review the lessons. I hope these can help others who got stuck like me.
If anyone has questions on specifics, let me know and I’ll try my best to explain!
Task 1: For the overused words, remove it every other time it appears.
// First, count overused words (This was also my solution to step 4)
overusedWords.forEach(word => {
let count = 0;
for (let i = 0; i < betterWords.length; i++) {
if (betterWords[i] === word) {
count++
};
// Second, remove overused words every other time
if (betterWords[i] === word && count % 2 === 0) {
betterWords.splice(i, 1);
}
};
console.log(`"${word}" count: ${count}`);
});
Task 2: Write a function that finds the word that appears the greatest number of times.
// First, account for capitalized words and remove special characters
let formattedWords = [];
let letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
betterWords.forEach(betterWord => {
betterWord = betterWord.toLowerCase();
betterWord = Array.from(betterWord);
const allLetters = betterWord.filter(element => letters.includes(element));
betterWord = allLetters.join('');
formattedWords.push(betterWord);
});
// Second, create an array made up of each word's frequency
let mostCommonCount = [];
formattedWords.forEach(formattedWord => {
formattedWordCount = 0;
for (m = 0; m < formattedWords.length; m++) {
if(formattedWords[m] === formattedWord) {
formattedWordCount++
}
}
mostCommonCount.push(formattedWordCount);
});
// Third, match the number of the highest word frequency with the word itself
let mostCommonWordCount = Math.max(...mostCommonCount);
let mostCommonWord = betterWords[mostCommonCount.indexOf(mostCommonWordCount)];
console.log(`The most common word is "${mostCommonWord}" which appears ${mostCommonWordCount} times`);
Task 3: Replaced overused words with something else.
// First, create an array of replacement words
let replacementWords = ['particularly', 'extraordinarily', 'pretty', 'so', 'almost'];
let replacementWordsCount = 0;
// Second, cycle through overused words while accounting for the first instance of each overused word that's not going to be replaced
overusedWords.forEach(word => {
let count = 0;
for (let i = 0; i < betterWords.length; i++) {
if (betterWords[i] === word) {
count++;
if (count > 1) {
replacementWordsCount++;
betterWords.splice(i, 1, replacementWords[replacementWordsCount-1]);
}
}
};
});