Mini Linter - step 8

Hello!

I would like to ask you about step 8 in Mini Linter project (https://www.codecademy.com/courses/introduction-to-javascript/projects/mini-linter).

I’m completely stuck with all three ideas:

  1. For the overused words, remove it every other time it appears.
  2. Write a function that finds the word that appears the greatest number of times.
  3. Replaced overused words with something else.

Are there any ways to solve it, using only the tools, which have been mentioned in previos lessons of “Introduction To JavaScript” course?

I kindly ask you to give examples of solutions, using only the tools, which have been mentioned in previos lessons of “Introduction To JavaScript” course.

I spent a sleepless night today… Please help me! I don’t want to spend one more sleepless night :joy: I won’t be able to sleep until the solution is found :man_facepalming:

5 Likes

Hi Shumil, I was able to find a way to solve for the first idea: “For the overused words, remove it every other time it appears”

let reallyCount = 0;
let veryCount = 0;
let basicallyCount = 0;

function even(num){
  return num % 2 === 0;
}

let idea1 = storyWords.filter(function(word) {
  if (!overusedWords.includes(word)) {
    return !overusedWords.includes(word)
  } else if (word === 'really') {
    reallyCount += 1;
    if(even(reallyCount)) {
      return word
    }
  } else if (word === 'very') {
    veryCount += 1;
    if (even(veryCount)) {
      return word
    }
  } else if (word === 'basically') {
    basicallyCount += 1;
    if (even(basicallyCount)) {
      return word
    }
  }
})
console.log('Prior to removing overused words every other time it appears: \nreally count: ' + reallyCount + '\nvery count: ' + veryCount + '\nbasically count: ' + basicallyCount +'\n');

console.log(idea1.join(' '));

// everything below this point is just to troubleshoot and double check that the code above
// worked and removed each 'odd' numbered instance of the overused words array
reallyCount = 0;
veryCount = 0;
basicallyCount = 0;

idea1.forEach(function(word) {
  if (word === 'really') {
    reallyCount += 1
  } else if (word === 'very') {
    veryCount += 1
  } else if (word === 'basically') {
    basicallyCount += 1
  }
});

console.log('\nAfter removing overused words every other time it appears \nreally count: ' + reallyCount + '\nvery count: ' + veryCount + '\nbasically count: ' + basicallyCount);

I basically added a nested IF function in each of the IF/ELSE statements that I created in step 4 to count the number of time each “overused word” was counted. In that nested function, i used a modulo to determine if the counter was even or odd. if it was even, then I returned the word to my idea1 array

3 Likes

Thank you very much!

1 Like

9 posts were split to a new topic: Getting is an empty array on task 1 of Mini-linter extra credit

Hi,

Here is how i did it,

const remove = (array,word) =>{
let counter = 0;
for(i = 0; i < array.length; i ++){
if(array[i] == word){
counter ++;
if(counter % 2 === 0){
array.splice(i, 1)
}
}

}
console.log(array)
console.log(counter);
};

const wordCount = (array,word) =>{
let counter =0;

for(i = 0; i < array.length; i ++){
if(array[i].toLowerCase().replace(/[’"]+/g, ‘’) == word.toLowerCase().replace(/[’"]+/g, ‘’)){
counter ++;
}
}
return counter
}

const mostUsedWord = (wordArray) =>{
let max = 0;
let result = ‘’;
for(j = 0; j < wordArray.length; j ++){

checkWord = wordArray[j];
let currentCheckWord = wordCount(wordArray,checkWord);

if( currentCheckWord > max){
max = currentCheckWord;
result = checkWord;
}
}

console.log(result)
console.log(max)
}

1 Like

Hello. Thank you for the solutions for 1 and 2. I was also stuck… I get the first one. The second one is still too complicated :stuck_out_tongue:
Here I got the solution for 3 all explained. I think it is the easiest way, no clue how to do it with array…

--------- code is below -----------

//define a new function for overused words replacement:
function replaceOverusedWords() {
//we save story.replace(…) in story:
story = story.replace(/really/gi, “definitely”);
story = story.replace(/very/gi, “so much”);
story = story.replace(/basically/gi, “fundamentally”);
// /really/gi is a regex. it means we will change the word globally (“g” in gi) and that it is case insensitive in our case (“i” in gi).
}
//function call:
replaceOverusedWords();
//finally we log in the console. it should write out our story with replaced words:
console.log(story);

Hi, that’s another solution for #1. Must admit I had a bit of help to get me going.

const fewerOverused = [ ];
let countingArray = [0,0,0];
storyWords.forEach (
word => {
let indexOfOverused = overusedWords.indexOf(word);
if (indexOfOverused >= 0) {
countingArray[indexOfOverused] = countingArray[indexOfOverused]+1;
}
if (!overusedWords.includes(word)) {
fewerOverused.push(word);
} else if (word === overusedWords[indexOfOverused] && countingArray[indexOfOverused] % 2 === 1) {
fewerOverused.push(word);
}
}
)
console.log (fewerOverused.reduce(
(accumulator, currentValue) => {
return accumulator + ’ ’ + currentValue;
}
)
)

Please, how do you print out elements in an array and their frequencies using a forEach loop?