Mini linter project / Extra tasks (last ones)

Hi there! I almost completed the excercise but I can’t figure out how to REMOVE EVERY OTHER WORD of the overusedWords from the betterWords array.

Here’s my code: (It’s wrong but I can’t figure out how to do it)

Basically I thought I would keep the count of the overusedWords and then remove those in an odd index position from the betterWords array, but obviously that does not work.

How would be your approach to do that?

Thanks in advance,

Happy coding!

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(' ');
//console.log(storyWords.length);

const betterWords = storyWords.filter(word => {
  if(unnecessaryWords.includes(word) === false) {
    return word;
  }
});
//console.log(betterWords);

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

const removeEveryOtherWord = betterWords.forEach(word => {

  if(word === overusedWords[0]) {
    reallyCount++;
  } else if (word === overusedWords[1]) {
    veryCount++;
  } else if (word === overusedWords[2]) {
    basicallyCount++;
  }

  for(i = 0; i < reallyCount.length; i++) {
    if(reallyCount[i] % 2 != 0) {
      betterWords.splice(reallyCount[i]);
    } else if (veryCount[i] % 2 != 0) {
      betterWords.splice(veryCount[i]);
    } else if (basicallyCount[i] % 2 != 0) {
      betterWords.splice(basicallyCount[i]);
    }
  }
});

you want to look at ALL, not specific ones.
you want to know whether it’s in that list, not whether it’s the first one in the list. or second or third or fourth.

to do something “every other” what you need to know is what you did for the previous one, kind of like how when you count from 0 to 10 you need to know the previous value to get the next

1 Like

Aww ok so the overused words should be removed every other time in general, or for each specific word?

ex. “I’m really exited for this, basically I was waiting my whole life.”
the two words are to be considered separatelly or, there are two overused words and the second one should be removed?

Didn’t you already answer that?

1 Like

I guess I’d like to consider every word separately, it makes more sense to do that for me.

Any hint on how to approach that?

But if they are to be consider as a whole, I’m happy to do that too. I’m sorry but it’s being a bit confusing to understand how i should resolve it.

I don’t know what that means.

Presumably you’d iterate through each word, and make a decision for each one.

And that decision is something you can describe in english, and that would say a whole lot about what code you’d put there.

I just thought it would make more sense to remove every other repeated word separately as the number of times each one appears is different.

So if the word rellay appears 7 times, I’d remove 4 of them (the ones in and odd index position)
and same with ‘very’ and ‘basically’.

Does it matter how many times it appears?

It’s either overused, or not.

Every other has to do with what order they appear in, how many there are … isn’t related

And you don’t have them in even/odd indices, you have them somewhere among other words. They could ALL be on even locations. All you care about is… what did you decide to do with the previous one?

You know you need to step through them all, so you’d write a loop. What’s the next thing you’d do? You’d consider whether or not it is overused. Then you’d consider whether or not you removed the previous one.

That correlates directly to for-statements and if-statements.

1 Like

My point is that I believe it makes more sense to delete every other word considering each word separately as the number of times each one appears is different.

So if you look at the whole final text, the over used words are equally ‘reduced’.

I’m sorry i can’t explain myself better, it’s just a bit confusing and maybe is due to the ammount of time I’ve been spending on the excercise.

It would be nice some guidance on how to approach the matter, that’s all. :grinning:

And my point is that it’s not relevant how many times it appears, so doing anything at all based on that information won’t help you.

What you do care about is what you did with the previous one. The amount? Not interesting, it could be infinite for all you care, since you’d start at the front and go forwards.

Keep, drop, keep, drop, keep, drop …

1 Like

Ok that makes more sense for me… I’ll try and understand how would that work.

Thank you!

For a particular word you might even keep all or none of it.

if you remove every other from this:

a b a b a b a b

then you have only a, or only b remaining depending on what you do with the first one

1 Like

Does this make sense?? It is not really working but i want to know if I’m on the right path.

let count = 0; 

const removeEveryOtherWord = overusedWords.forEach(word => {
  for(i = 0; i < betterWords.length; i++) {
    if(word === betterWords[i]) {
      count++;
    } if(count % 2 === 0) {
      betterWords.splice(betterWords[i])
    }
  }
});

I believe I’m not applying the .splice() method properly…

Do you really mean to do this?

a and b are both overused.

a b a b a b a

becomes

a b - - a b -

where - is where removals happened


not sure why you use both for-loop and forEach
use one or the other, no reason to use both, they do the same thing so if you decide to do it in one place then you’d use it in the other place for the same reason


also I’m not sure you know what Array.prototype.splice does…

if you want to copy from one source to another array but skipping some, then you’d iterate through the source, and append each value to the result array:

const stuff = [1, 2, 3]
const res = []
for (const thing of stuff) {
  res.push(thing)
}

the decision you have is, for the current thing, should it, or should it not, be appended to the result? so you have a condition, and an action for yes and an action for no, and that’s what an if-statement does

const stuff = [1, 2, 3]
const res = []
for (const thing of stuff) {
  if (should keep) {
    res.push(thing)
  }
}
1 Like

Also test your strategy using pen and paper or in your head before you write any code whatsoever. Writing code isn’t going to help you figure out what you’re doing, you need to have that figured out already.

And when you have figured out what you’re doing, you have a reference for writing your code. You won’t have a whole lot of options, there’s something specific that should happen.

Stick to basic array operations like append and iterate and lookup by index. There’s probably no reason to use anything other than that, and even if there is then you can implement it using those operations.

1 Like

Ok I got it to work now but not every word separated, it eliminates every other word in the order they appear.

let newStory = []; 
let count = 0;

const removeEveryOtherWord = betterWords.forEach(word => {
  if(!overusedWords.includes(word)) {
    newStory.push(word);
  } else {
    count++;
    if(count % 2 != 0) {
      newStory.push(word);
    }
  }
});

What do you think about it?

Now I’ve got think how to do it for every word separately

don’t know what that means. it’s vague.

1 Like

array = [‘really’, ‘very’, ‘really’]

outcome with the function I’ve made= [‘really’, ‘really’]

desired outcome = [‘really’, ‘very’]

Hope it makes more sense now, i want my function to return somethign like the desired outcome array…

I could probably come up with six different ways to obtain that result, that still doesn’t say what you mean should happen, you’re missing specifics to narrow it down to a single possible thing.

here’s a valid implementation of your specification:

function (xs) {
  return [xs[0], xs[1]]
}

Oh you want to remove every other of EACH overused word, independently from all other words.

Then you’d keep one counter for each word, no?

1 Like