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 into betterWordsStats
- 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(' '))
I’ll make a deal with you. You give me feedback on mine and I’ll give you feedback on yours. But no rush. I understand you might be busy. I’m looking for someone to critique my code and to give me feedback on how to improve. I will try to do the same. If you’d like. Deal?
Yes for sure! Would love to take a look
K. Here it is. I hope my comments give a good explanation of what my code does.
// This is the original story
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.';
// Array of over used words
let overusedWords = ["really", "very", "basically"];
// Array of unnecessary words
let unnecessaryWords = ["extremely", "literally", "actually"];
// Array of each word in the story
const storyWords = story.split(" ");
// Logging to the console the word count
console.log(`Your original story had ${storyWords.length} words.`);
// Array of story words with unnecessay words filtered out
const betterWords = storyWords.filter((word) => {
if (!unnecessaryWords.includes(word)) {
return word;
};
});
// Save to a variable the word count of betterWords array
const betterWordsCount = betterWords.length;
// This function takes in an array of words and returns a new array of each word that is in the overUsedWords array
const overUsedWordCount = (arrayOfWords) => {
let arrOfEachOverUsed = [];
for (let i = 0; i < arrayOfWords.length; i++) {
if (overusedWords.includes(arrayOfWords[i])) {
let wordIndex = overusedWords.findIndex((word) => {
return word === arrayOfWords[i];
});
let word = overusedWords[wordIndex];
arrOfEachOverUsed.push(word);
}
}
return arrOfEachOverUsed;
};
// Saved the return value of calling the overUsedWordCount function with betterWords as an argument
const arrOfEachWord = overUsedWordCount(betterWords);
// Defined a function that takes an array of words and returns an object with the words as keys and the word count for each word as values
const arrayIntoObject = (arrayOfWords) => {
const object = {};
arrayOfWords.forEach((word) => {
if (!(word in object)) {
object[word] = 1;
} else {
object[word] = object[word] + 1;
}
});
return object;
};
// Saved the return value of calling arrayIntoObject function with arrOfEachWord as an argument
const objectWithWordCount = arrayIntoObject(arrOfEachWord);
// This function takes in an object with words as the key and returns an array of strings of how many times they used each word which is the values of the keys.
const objIntoStrings = obj => {
let array = []
for (let key in obj) {
array.push(`You used the word ${key} ${obj[key]} time(s).`);
};
return array;
};
// calls a function that takes in an object with words as keys and word count as the values and returns how many times they used each word and saved it to a variable.
const overUsedWordsStrings = objIntoStrings(objectWithWordCount);
// This function takes in an array of words and counts how many periods(.) and exclamation points(!) there, effectively counting the sentences.
const sentenceCounterFunc = arrOfWords => {
const period = '.';
const exclmPoint = '!';
let count = 0;
arrOfWords.forEach((word) => {
if (word[word.length - 1] === period || word[word.length - 1] === exclmPoint) {
count++;
};
});
return count;
};
// Saved into a variable the return value of calling sentenceCounter with betterWords as an argument
const sentenceCount = sentenceCounterFunc(betterWords);
/**This function will need the following variables as parameters;
*
* betterWordsCount - a number,
* sentenceCount - a number,
* overUsedWordsString - an array of strings
*
* Will take all these variables and logs them all to the console in a formatted string.
*/
const loggingFunction = (wordCount, sentenceCount, arrayOfStrings) => {
console.log(`Your new word count is ${wordCount}.\nYour sentence count is ${sentenceCount}.`);
arrayOfStrings.forEach((string) => {
console.log(string);
});
};
// Calls the above function with the appropriate variables as arguments
loggingFunction(betterWordsCount, sentenceCount, overUsedWordsStrings);
// Turns betterWords array into a string and saves it into a variable
const betterStoryWords = betterWords.join(' ');
// Logs to the console betterStoryWords
console.log(betterStoryWords);
/**Congratulations! You’ve improved the original paragraph and given the user some important information about his or her work. Think about ways in which you can extend this project, potentially by using other JavaScript knowledge you have.
Here are some ideas:
For the overused words, remove it every other time it appears.
Write a function that finds the word that appears the greatest number of times.
Replaced overused words with something else. */
I was planning on doing number 8 the last problem a little later.
1 Like
I left comments in the your code.
/**You should get in the habit of leaving comments in your code to explain what each line is doing. It makes it easier for future you as well as other people who will eventually read your code. Plus too this is a highly technical field. Learning to code. Sometimes you can go down a rabbit hole and you must leave bread crumbs for others to follow your steps. I am currently breaking myself out of the habit of coding and caring who sees my code afterward. But I have to remember if I plan to work in a company I will most likely collaborate with others. Or other people might have to debug my code. It just makes everyones life easier. */
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"];
// https://flaviocopes.com/javascript-automatic-semicolon-insertion/
const storyWords = story.split(" ");
let betterWords = storyWords.filter((word) => {
return unnecessaryWords.indexOf(word) === -1;
});
// like how you anticipated that you would need an object to store all the info on the story.
let betterWordsStats = {};
// Populating the object with info. Nice.
betterWordsStats.words = betterWords.length;
function getOverusedWords(input, filter) {
// looks like you are setting the stage for each word in the filter to get an object with occurences and the index of each occurence.
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)
// }
// }
// }
// alternate way to accomplish the same task. Not sure which way is more efficient.
input.forEach((word, index) => {
// used a forEach on input
if (filter.includes(word)) {
// conditional that checks if the filter array has word
for (let key in betterWordsStats) {
// for..in loop iterates over object
if (key === word) {
// conditional that checks if the key is equal to the word
// same as your code above sort of.
betterWordsStats[key].occurences += 1;
betterWordsStats[key].locations.push(index);
}
}
}
});
}
// This seems pretty straight forward. Your checking to see if each element in the array has a period or exclamation point and taking a count for every occurence. Then your storing in the object with all the stats.
function getSentenceCount(input) {
let sentences = 0;
input.forEach((word) => {
// I did something simular but I used word[word.length - 1] === '.' || '!'
// I guess its the same thing
if (word.endsWith(".") || word.endsWith("!")) {
sentences += 1;
}
});
betterWordsStats.sentences = sentences;
}
// This is pretty impressive. I'm not sure I could have solved this any better.
function getMostCommonWord(input) {
let mostCommonWord = "";
let mostCommonCount = 0;
// I noticed that you used betterWords.length but wouldn't it be better if you used input.length. In this case aren't they the same thing. I think the difference is that you could reuse this function for other arrays of strings.
for (let i = 0; i < input.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,
};
}
// nice
function removeOverused(input, filter) {
let arrWithoutOverUsed = input.filter((word) => {
return !filter.includes(word);
});
betterWords = arrWithoutOverUsed;
}
// awesome
function replaceWord(input, filter, replacement) {
let arrReplaceOverused = input.map((word) => {
return filter === word ? replacement : word;
});
betterWords = arrReplaceOverused;
}
console.log("this is here");
// I like how you place your all the finished products at the the bottom of your code so as to not get reference errors.
//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(' '))
// Good job man. Hope I was helpful.
1 Like
Thank you, that’s really helpful, especially:
- note on comments - 100%, I will start doing this!
- the
for...in
- hadn’t used this and it’s perfect for getOverusedWords()
- like the way you have refactored this
- on
getMostCommonWord()
changing to input.length
great catch
Here’s your code with notes. Main points
- I add another layer to your object again:
really : 2
becomes really : {occurences : 2}
- Loved the way you checked for an object key then set it if it didn’t exist or incrememented it if it did, i had a real challenge with that myself
- I felt that
arrOfEachWord
didn’t need to be defined in the global scope. Instead, I made arrayIntoObject()
call it as a helper function, and made a couple of changes to the function so that it flowed through
// This is the original story
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.';
// Array of over used words
let overusedWords = ["really", "very", "basically"];
// Array of unnecessary words
let unnecessaryWords = ["extremely", "literally", "actually"];
// Array of each word in the story
const storyWords = story.split(" ");
// Logging to the console the word count
console.log(`Your original story had ${storyWords.length} words.`);
// Array of story words with unnecessay words filtered out
// Much more readable than my unnecessaryWords.indexOf(word) === -1, really like it
const betterWords = storyWords.filter((word) => {
if (!unnecessaryWords.includes(word)) {
return word;
};
});
// Save to a variable the word count of betterWords array
const betterWordsCount = betterWords.length;
// This function takes in an array of words and returns a new array of each word that is in the overUsedWords array
// Good use of overusedWords from the global scope
const overUsedWordCount = (arrayOfWords) => {
let arrOfEachOverUsed = [];
for (let i = 0; i < arrayOfWords.length; i++) {
if (overusedWords.includes(arrayOfWords[i])) {
let wordIndex = overusedWords.findIndex((word) => {
return word === arrayOfWords[i];
});
let word = overusedWords[wordIndex];
arrOfEachOverUsed.push(word);
}
}
return arrOfEachOverUsed;
};
// Saved the return value of calling the overUsedWordCount function with betterWords as an argument
// REMOVED below, no need to call this helper function into the global scope
//const arrOfEachWord = overUsedWordCount(betterWords);
// Defined a function that takes an array of words and returns an object with the words as keys and the word count for each word as values
// I think the count makes more sense as property of the overused word, I would be tempted to add more properties of these words in future versions, such as 'word location'.
//const arrayIntoObject = (arrayOfWords) => {
// const object = {};
// arrayOfWords.forEach((word) => {
// if (!(word in object)) { <----- I really like this method of conditional key creation and will be copying it
// object[word] = 1;
// } else {
// object[word] = object[word] + 1;
// }
// });
// return object;
//};
// As above, refactored to make occurences a key-value pair in the object
const arrayIntoObject = (arrayOfWords) => {
let overusedArr = overUsedWordCount(arrayOfWords) //calling the helper function here and assigning it a value
const overUsedWordsObj = {};
overusedArr.forEach((word) => { // updated here to pick up the new `overusedArr ` object
if (!(word in overUsedWordsObj)) {
overUsedWordsObj[word] = {};
overUsedWordsObj[word].occurence = 1;
} else {
overUsedWordsObj[word].occurence += 1;
}
});
return overUsedWordsObj;
};
// Saved the return value of calling arrayIntoObject function with arrOfEachWord as an argument
const objectWithWordCount = arrayIntoObject(betterWords); // updated to give the argument you need on line 30
// This function takes in an object with words as the key and returns an array of strings of how many times they used each word which is the values of the keys.
const objIntoStrings = obj => {
let array = []
for (let key in obj) {
array.push(`You used the word ${key} ${obj[key].occurence} time(s).`); //updated to get the nested value
};
return array;
};
// calls a function that takes in an object with words as keys and word count as the values and returns how many times they used each word and saved it to a variable.
const overUsedWordsStrings = objIntoStrings(objectWithWordCount);
// This function takes in an array of words and counts how many periods(.) and exclamation points(!) there, effectively counting the sentences.
const sentenceCounterFunc = arrOfWords => {
const period = '.';
const exclmPoint = '!';
let count = 0;
arrOfWords.forEach((word) => {
if (word[word.length - 1] === period || word[word.length - 1] === exclmPoint) {
count++;
};
});
return count;
};
// Saved into a variable the return value of calling sentenceCounter with betterWords as an argument
const sentenceCount = sentenceCounterFunc(betterWords);
/**This function will need the following variables as parameters;
*
* betterWordsCount - a number,
* sentenceCount - a number,
* overUsedWordsString - an array of strings
*
* Will take all these variables and logs them all to the console in a formatted string.
*/
const loggingFunction = (wordCount, sentenceCount, arrayOfStrings) => {
console.log(`Your new word count is ${wordCount}.\nYour sentence count is ${sentenceCount}.`);
arrayOfStrings.forEach((string) => {
console.log(string);
});
};
// Calls the above function with the appropriate variables as arguments
loggingFunction(betterWordsCount, sentenceCount, overUsedWordsStrings);
// Turns betterWords array into a string and saves it into a variable
const betterStoryWords = betterWords.join(' ');
// Logs to the console betterStoryWords
console.log(betterStoryWords);
/**Congratulations! You’ve improved the original paragraph and given the user some important information about his or her work. Think about ways in which you can extend this project, potentially by using other JavaScript knowledge you have.
Here are some ideas:
For the overused words, remove it every other time it appears.
Write a function that finds the word that appears the greatest number of times.
Replaced overused words with something else. */
// Overall I thought your code was great here, one change for the object and one change for the helper function