Mini Linter / Iterators / HELP JS


#1

Project Mini Linter here: https://www.codecademy.com/courses/learn-javascript-iterators/projects/mini-linter?course_redirect=introduction-to-javascript

I need a help with this project in item 8

8
For the overused words, remove it every other time it appears.

I even don’t know how to start.
I came up with something like that: make a new array, count overusedWords, remove overused words if they appear more than once and then throw away what’s left from story to new array. But i dont have idea how to write it.

Someone, somebody, some help :wink:


#2

Did you check the hint? It gives a pretty extensive hint on how to overcome to problem

i personally thought using filter and includes was a good idea, not sure though

creating a new array isn’t a bad idea, you could loop over storyWords, then loop over overusedWords to if the current word of story words equals one of the over used words, if so, do nothing, else append to your new array


#3

im stuck here


let removeWords = [];

let countReally = 0;
let countVery = 0;
let countBasically = 0;

for (let storyWordsIndex = 0; storyWordsIndex <= storyWords.length; storyWordsIndex++) {
    current_word = storyWords[storyWordsIndex];
 

    if ('really' === current_word ) {
	  	countReally += 1;
    };
  
  	if ('very' === current_word ) {
	  	countVery += 1;
   	};
		
  	if ('basically' === current_word ) {
	  	countBasically += 1;
   	};

    if( !(countReally > 1 && current_word === 'really') ||						
        !(countVery > 1 && current_word === 'very') ||
        !(countBasically > 1 && current_word === 'basically')) {
      		removeWords.push(current_word);   
    }
};

console.log(removeWords.join(' '))

The last if doesn’t work correctly and i dont know why? i tried everything: || && else/if, split on smaller if and nothing, always something is wrong.
I will try Your suggestion tomorrow, thanks btw.
Now im going to bed, my head almost exploded :exploding_head:
bye bye :sleeping:


#4

While you were busy on Number 1, I went to work on Number 2. Still don’t have it wrapped in a single function, but the proof of concept works as expected…

Write a function that finds the word that appears the greatest number of times.

https://repl.it/Lfl7

The word at sortArray[0][0] is the one that appears the most. ['the', 11].


#5

why are you complicating matters so much? if you use if to check if current_word equals 'really', then use else if to check if current_word equals 'very' and another else if to check if current_word equals basically we can use else to push current_word to removeWords


#6

bit confused? The instructions ask us to give used how many times each word in overusedWords array is used? i was lazy and did:

let counting = storyWords.filter(word=>
overusedWords.includes(word)
).length

but then it doesn’t count each individual overused word


#7

This works correctly, and when I did that way else/if it works too :slight_smile:

let removeWords = [];

let countReally = 0;
let countVery = 0;
let countBasically = 0;

for (let storyWordsIndex = 0; storyWordsIndex <= storyWords.length; storyWordsIndex++) {
current_word = storyWords[storyWordsIndex];

	// Counters

if ('really' === current_word ) {
  	countReally += 1;
};

if ('very' === current_word ) {
  	countVery += 1;
};

if ('basically' === current_word ) {
  	countBasically += 1;
};


// Next section

if(countReally > 1 && current_word === 'really') {
  continue;
}

if(countVery > 1 && current_word === 'very') {
  continue;
}

if(countBasically > 1 && current_word === 'basically') {
  continue;
}

removeWords.push(current_word);

};

console.log(removeWords.join(’ '));

:fireworks:


#8

For that we would need a nested loop to iterate over both words and overUsedWords. Or, build a frequency table (an object) of overUsedWords. It could get clever and include an array as a value, rather than a count. The array would contain the indexes of the found words from which removals could be determined.


#9

Mini Linter https://www.codecademy.com/courses/learn-javascript-iterators/projects/mini-linter?course_redirect=introduction-to-javascript

Number 3 Replaced overused words with something else.

i wrote this:

let replaceCurrentWords = [];

for (let i = 0; i <= storyWords.length; i++) {
currentWord = storyWords[i];

	if ('really' === currentWord) {
		 currentWord = currentWord.replace('really', 'AAA')
   
   replaceCurrentWords.push(currentWord);
  
   console.log(replaceCurrentWords.join(' '))
}

};

and it returns

AAA
AAA AAA

Why people, why? :smiley:


#10

ok, i see the problem, .push and console.log were in wrong place :stuck_out_tongue_winking_eye:

let replaceCurrentWords = [];

for (let i = 0; i <= storyWords.length; i++) {
currentWord = storyWords[i];

	if ('really' === currentWord) {
		 currentWord = 'LEARN_AlICE_LEARN';
}

 replaceCurrentWords.push(currentWord);

};

console.log(replaceCurrentWords.join(’ '))


#11

Finding multiple occurences is simple enough to do with basic iteration, though JS does give us a couple of useful tools, indexOf and lastIndexOf. Combine the latter with string slicing and we can build an index of found targets

// Given earlier `story` object and these lines,

let stripped = story.split('');
let letters = stripped.filter(x => /\w|\s/.test(x)).join('');
let words = letters.split(' ');

// Build a frequency histgram of overused words

let overUsedWords = ['really', 'very', 'basically'];
const ouw_freq = {};
for (let i = 0; i < overUsedWords.length; i++) {
  let word = overUsedWords[i]
  let text = words.slice();
  let idx, index = [];
  do {
    idx = text.lastIndexOf(word);
    if (idx + 1) {
      index.push(idx);
      text = text.slice(0, idx);
    }
    else break;
  } while (true);

  ouw_freq[word] = index;
}
 > 
=> [ 54 ]
 > ouw_freq
=> { really: [ 82, 39 ],
  very: [ 183, 159, 125, 117, 95 ],
  basically: [ 54 ] }
 >

Earlier repl has some code commented out to test this.

https://repl.it/Lfl7/1


ouw_freq[word] = index;

is not really useful, since once we start removing words this index will be useless. That suggests removing each word as we analyze it.

The neat thing is the index is in reverse so we can leave one of the first two occurences and every other after that.

if there is 1 occurence, remove it
if there are 2 then remove one
if there are 3 then remove two
if there are 4 then remove two
if there are 5 then remove three.
and so on...

Will work on an implementation of the idea and post back.


Something of a proof of concept…

https://repl.it/Lfl7/2


#12

Here is a breakdown of the mysterious looking lines,

let chars = story.split('');
let letters = chars.filter(x => /\w|\s/.test(x)).join('');
let words = letters.split(' ');

We start with a list of all characters.

let chars = story.split('');

Next we filter out only word characters and spaces, then join the filtered characters back into a text block.

let letters = chars.filter(x => /\w|\s/.test(x)).join('');

Finally we create an array of the words in the filtered text.

let words = letters.split(' ');

The only thing we haven’t done is convert case so there could be The and the in the frequency table.

Extra reading will be needed to brush up on Regular Expressions and the .test() method of the RegExp object.

Playing out…

https://repl.it/Lfl7/3


#13

number 2 Write a function that finds the word that appears the greatest number of times.

Result gives me: the, but i dont know what to do with ‘The’ starting with upperCase???

let store = storyWords;
let frequency = {};
let max = 0;
let result;

for(let word in store) {
word_from_store = store[word];
frequency[word_from_store]=(frequency[word_from_store] || 0) + 1;
if(frequency[word_from_store] > max)
max = frequency[word_from_store];
result = word_from_store;
}
}
console.log(result)


#14

i change story to lowerCase like this:

let lowerCaseStory = story.toLowerCase();
console.log(story.toLowerCase())

then change this:

let storyWords = lowerCaseStory.split(’ ');

OR ANOTHER WAY
change
word_from_store = store[word];
to
word_from_store = store[word].toLowerCase();


#15

Here we filter only word characters and spaces, and deal with uppercase…

let words = story.split('');
    words = words.filter(x => /\w|\s/.test(x)).join('').toLowerCase();
    words = words.split(' ');
Most frequently used word: the
Number of occurences:      14

Good one. Is that your own logic, or learned online? Nonetheless, I’m going to borrow it.


Done (the borrowing part).

https://repl.it/Lfl7/4


#16

updating

word_from_store = store[word].toLowerCase().replace(’"’, ‘’);


#17

Huh?


Aside

We should avoid getting addicted to tossing in variables of any sort. They do not clear the way, but clutter it.

Center on the specific object and let it become a transformation of itself wherever possible so memory leakage is kept to a minimum, AND we can keep track of what needs to be manually garbage collected afterward.


#19

I’m not in an oppressive environment, but I know the mental challenges well. We succeed when we not only persevere, but when we ourselves become precocious beyond anyone’s control. Become yourself.


Of the husband part… Is he helping, or hindering? Ask him to stand down and advise only, not direct. In programming, we are the directors.


#21

The better to remain reticent in personal regards.


#24