Hi There, would love to hear your takes on my Mini Linter!
I’ve tried to make the program really flexible, so that I could feed in different arrays of overused words and still have it work, etc.
Any general feedback is greatly appreciated, and I’m particularly unsure about:
-
function getOverusedWords()
which feels quite clunky. There must be a cleaner way to generate that object and push it back intobetterWordsStats
- The number of iteration loops. I’ve tried to keep it down, but I’m iterating through the whole story A LOT. Is there a more efficient approach?
Thanks in advance:
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' ];
const storyWords = story.split(' ')
let betterWords = storyWords.filter(word => {
return unnecessaryWords.indexOf(word) === -1
});
let betterWordsStats = {};
betterWordsStats.words = betterWords.length
function getOverusedWords(input,filter) {
for (let i = 0; i < filter.length; i++) {
betterWordsStats[filter[i]] = {},
betterWordsStats[filter[i]].occurences = 0,
betterWordsStats[filter[i]].locations = []
}
for (let k = 0; k < input.length; k++) {
for (let j = 0; j < filter.length; j++) {
if (input[k] === filter[j]) {
betterWordsStats[filter[j]].occurences += 1
betterWordsStats[input[k]].locations.push(k)
}
}
}
}
function getSentenceCount(input) {
let sentences = 0
input.forEach(word => {
if (word.endsWith('.') || word.endsWith('!')) {
sentences += 1;
}
})
betterWordsStats.sentences = sentences
}
function getMostCommonWord (input) {
let mostCommonWord = '';
let mostCommonCount = 0
for (let i = 0; i < betterWords.length; i++) {
let runningCount = 0
for (let j = 0; j < input.length; j++) {
if (input[i] === input[j]) {
runningCount++
}
}
if (runningCount > mostCommonCount) {
mostCommonWord = input[i];
mostCommonCount = runningCount
}
}
betterWordsStats['Most Common Word'] = {
'word' : mostCommonWord,
'count' : mostCommonCount }
}
function removeOverused (input,filter) {
let arrWithoutOverUsed = input.filter(word => {
return !filter.includes(word);
});
betterWords = arrWithoutOverUsed
}
function replaceWord (input,filter,replacement) {
let arrReplaceOverused = input.map(word => {
return filter === word ? replacement : word
})
betterWords = arrReplaceOverused
}
//removeOverused(betterWords,overusedWords)
replaceWord(betterWords,'really','quite')
betterWordsStats.words = betterWords.length;
getSentenceCount(betterWords)
getOverusedWords(betterWords,overusedWords)
getMostCommonWord(betterWords)
console.log(betterWordsStats)
console.log(betterWords.join(' '))