Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created Sep 24, 2021
Embed
What would you like to do?
Codecademy export
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' ];
// 1.
const storyWords = story.split(' ');
// 2.
const wordCount = storyWords.length;
// 3.
const betterWords = storyWords.filter(word => !unnecessaryWords.includes(word));
// 4.
const overusedWordsCounter = {};
betterWords.forEach(word => {
if (overusedWords.includes(word)) {
if (word in overusedWordsCounter) {
overusedWordsCounter[word] += 1;
} else {
overusedWordsCounter[word] = 1;
}
}
});
// 5.
let sentenceCounter = 0;
storyWords.forEach(word => {
if (word[word.length - 1] === '.' || word[word.length - 1] === '!') {
sentenceCounter++;
}
});
// 6.
const logAllCounters = () => {
console.log(`word count: ${wordCount}, sentence count: ${sentenceCounter}, overused words:`);
console.log(overusedWordsCounter)
};
logAllCounters();
// 7.
console.log(betterWords.join(' '));
// 8. - For the overused words, remove it every other time it appears.
const removeEverySecondOverusedWord = (storyWords, overusedWords) => {
const overusedWordsCounter = {};
return storyWords.filter(word => {
if (overusedWords.includes(word)) {
if (word in overusedWordsCounter) {
delete overusedWordsCounter[word];
return false;
} else {
overusedWordsCounter[word] = 1;
return true;
}
} else {
return true;
}
}).join(' ');
};
//console.log(removeEverySecondOverusedWord(storyWords, overusedWords));
// 8. - Write a function that finds the word that appears the greatest number of times.
const findWordWithMostCounts = (storyWords) => {
const wordsCounter = {};
storyWords.forEach(word => {
if (word in wordsCounter) {
wordsCounter[word] += 1
} else {
wordsCounter[word] = 1
}
});
let highestCount = 0;
let wordWithMostCounts = '';
for (const word in wordsCounter) {
if (wordsCounter[word] > highestCount) {
highestCount = wordsCounter[word];
wordWithMostCounts = word;
}
};
return `Word: ${wordWithMostCounts} appears the most times (${highestCount}).`;
}
//console.log(findWordWithMostCounts(storyWords));
// 8. - Replaced overused words with something else.
const replaceOverusedWords = (arr, wordToReplace, replacement) => {
return arr.map(word => word === wordToReplace ? replacement : word).join(' ');
};
//console.log(replaceOverusedWords(storyWords, 'very', 'quite'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment