Here are my implementation to the project of “Mini Linter” with assisting of instructions attached to it, stack overflow and some other code snippets from other places,
// text assignment
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.';
// split text into single words
let storyWords = story.split(' ');
console.log(storyWords);
// define overused words
let overusedWords = ['really', 'very', 'basically'];
// define unnecessary words
let unnecessaryWords = ['extremely', 'literally', 'actually'];
//filter the unnecessary words out of text in story words array then assign the result to betterWords array
let betterWords = storyWords.filter(function(word) {
return !unnecessaryWords.includes(word);
})
console.log(betterWords) // printing out the text without the unnecessary words
// count overused words
let reallyCount = 0;
let veryCount = 0;
let basicallyCount = 0;
// counting the overused words
for (word of storyWords) {
if (word === 'really') {
reallyCount += 1
} else if (word === 'very') {
veryCount += 1
} else if (word === 'basically') {
basicallyCount += 1
}
}
// count sentences
let sentenceCount = 0;
//counting the number of sentences in the text
for ( wordEnd of storyWords) {
if (wordEnd[wordEnd.length - 1] === '.' || wordEnd[wordEnd.length - 1] === '!') {
sentenceCount += 1
}
};
// Defin function logging out some statistics about the text
const logInfo = (param1, param2, param3, param4, param5) => {
param1 = storyWords.length;
//word no
console.log('word Count :' + param1);
//printing out the number of the sentences in the text
console.log(`The number of the sentences in the text is ${param2}`)
// printing out the overused words
console.log('really :' + param3);
console.log('very :' + param4);
console.log('basically :' + param5);
};
// function call with 5 arguments
logInfo( storyWords.length,sentenceCount, reallyCount, veryCount, basicallyCount)
//console.log(betterWords.join(' '));
// Defin a random index to choose a word from a set of arrays of replacing words
let randomIndex = Math.floor(Math.random() * 4);
//replace the overused words in the text with some words chosen randomly from three arrays
//replace "really" and printing out the text
while(storyWords.indexOf('really') != -1) {
const reallyReplacements = ['truly', 'absolutely', 'certainly'];
storyWords.splice(storyWords.indexOf('really'), 1, reallyReplacements[randomIndex]);
}
//replace "very" and printing out the text
while(storyWords.indexOf('very') != -1) {
const veryReplacements = ['tremendously', 'greatly', 'decidedly'];
storyWords.splice(storyWords.indexOf('very'), 1, veryReplacements[randomIndex]);
}
//replace "basically" and printing out the text
while(storyWords.indexOf('basically') != -1) {
const basicallyReplacements = ['essentially', 'fundamentally', 'primarily'];
storyWords.splice(storyWords.indexOf('basically'), 1, basicallyReplacements[randomIndex]);
}
// printing out the array text as a whole formatted text separated by spaces
console.log(storyWords.join(' '));
//find the most frequent word in the text
console.log(findMostRepeatedWord(story)); // Result: "the"
// find the most frequent word in the text array
function findMostRepeatedWord(story) {
let words = story.match(/\w+/g); //match the who;e text
console.log(words);
// creat an object contains every words with their matching number
let occurances = {};
//looping through the words array to count every word frequency
for (let word of words) {
if (occurances[word]) {
occurances[word]++;
} else {
occurances[word] = 1;
}
}
console.log(occurances); // printing out the frequency of each word in the text
//finding out the most repeated word in the text
let max = 0;
let mostRepeatedWord = '';
for (let word of words) {
if (occurances[word] > max) {
max = occurances[word];
mostRepeatedWord = word;
}
}
//print out the most repeated word in the text
return `The most repeated word in the text is : ${mostRepeatedWord}`;
}