Compare array items to String #Mini Linter Project #step 3

Hi i want to compare a string to an array; then create a new array which excludes the matching items from the 2 previously compared data. I know how to do this using .filter() and .includes() but codecademy says that are 7 different ways to accomplish this, can people explain to me the other ways.

Exercise;

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.’ ;

let overusedWords = [‘really’, ‘very’, ‘basically’];

const storyWords = story.split(’ ')

Question: There is an array of words that are unnecessary. Iterate over your array to filter out these words. Save the remaining words in an array called betterWords . There are several ways that you could achieve this.

7? several?

conceptually it’s a filtering action, I wouldn’t say there are several ways to do it, there may be several ways to type it in code but that doesn’t seem especially important


.filter and .includes aren’t things on their own, you have to mention which types they belong to for that to mean anything

Hi thanks for getting back, can you share the other ways , you would do it.

I tried something the below (which i know is wrong) but not sure of any other methods

const betterWords = storyWords.filter(word => {
if ( word === overusedWords ) {
return betterWords
}
})

Can you see the errors in that code?

What confuses me most is why

  1. betterWords = storyWords.filter(word =>{ return !overusedWords.includes(word)}) works

but

betterWords = storyWords.filter(word =>{ return !word.includes(overusedWords)}) does not.

I’m guessing my logic of how .includes is not entirely correct. As i would assume console.log(storyWords.includes(overusedWords) ) would return true, but instead it returns false. Which i dont get as storyWords does includes overusedWords

…does it? surely you mean that storyWords includes some of the values that are found in overusedWords, but that’s not to say that it includes overusedWords itself

you can’t view this in the sense of “does this somewhat make sense when put in a sentence”, you will need to take a stricter view of what it actually does

by, ‘works’, do you mean it passes, or it works without errors on all inputs?

and yeah, “works” is often a word that you will need to examine closer so that you are instead speaking of specific differences

how exactly array’s includes method behaves can be read about in mdn’s javascript documentation (you have no excuse to say “i don’t know how this works” … go read about it, whatever it is). but if you think about what the word means then there aren’t all that many things it can do. you may want to test it out with simpler data.

you may even want to implement it yourself. it isn’t a complicated function, it’s one of the simpler things you can write that is still useful. but it would force you to process what it does - it may be that you are bypassing the meaning/purpose of it.

indeed, most things that you ever use should be things that you know how to implement yourself. the exceptions to this are mostly difficult libraries where you understand their interface but their inner workings are too difficult (such as cryptographic functions)

the reason you’d use built-in functions is usually not so that they can do a difficult task for you, but so that you don’t have to reimplement common details and focus on whatever problem it is you’re solving - combining simple functions allows you to make more powerful ones like how if you can get digits from a number, and you can get sum from an array, then you can combine those into a function that computes the sum of digits

Something you’ve done more than once is mixing up the shapes of things.

Is a number equal to an array of numbers?
It’s not, it’s not even the same shape, much less an identical value. (One thing cannot be equal to many things)
What you would instead need to do is consider each value in the array, compare each of them to the number, and if there was a match then you would have found that, yes, this number is present in this array. (One thing can be equal to one of many things)

Again, implementing the thing in question is a good way to force yourself to process the action in full. Pen and paper can work too by putting all the information on paper and then considering and carrying out operations on that information.

What doesn’t work though, is using daily speech and checking whether that sounds like another human would understand it.

a = `a quick brown fox jumps over the lazy dog`
s = `abcdefghijklmnopqrstuvwxyz`
 > s.split('').forEach(x => console.log(a.includes(x)))
   (26) true
<- undefined
1 Like

Hi I have read mdn many times on .includes( ) but I must be missing something. Thanks for your replies, they’re really helpful.

so this was the Exercise;

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.’ ;

let overusedWords = [‘really’, ‘very’, ‘basically’];

const storyWords = story.split(’ ')

**Question: There is an array of words that are unnecessary. Iterate over your array to filter out these words. Save the remaining words in an array called betterWords .

so i’ve used the below;
betterWords = storyWords.filter(word =>{ return !overusedWords.includes(word)})

this works as it returns the array betterWords without the items from overusedWords. However,
i want to know why – betterWords = storyWords.filter(word =>{ return !word.includes(overusedWords)}) — did not work

Well, why would it?

You may want to break out the anonymous function and look at that by itself.
How does that function need to behave in order to say what should stay and what should go?
Just because you’re using filter/map/whatever doesn’t mean the definition of your predicate has to be defined in the same place or that it’s all a single entity.

function shouldThisBeKept(something) {
}

And, given that you should know what each individual operation in your code does (if not, then that’s reason to stop and obtain this knowledge before coming back to the bigger problem) – then you should be able to execute it in your head and see where it goes wrong. If that sounds crazy, then that’s part of the problem, not realizing the degree of control and understanding you need of every single thing you’re writing – you are the one who is aligning these parts so that they match up with each other to form a connection from some input to some desired output and this takes a lot of care and precision.

Breaking out the anonymous function into its own separate thing allows you to consider it by itself in isolation from anything else. Now you need to consider how it needs to relate to the use of filter, and then separately how it can fulfill that behaviour – it becomes simpler subproblems instead of one big problem that is difficult.

For array.filter the function that you provide should look at each element of the array (passed to the function one at a time, separately)
so that means that you need a function that looks at one thing, and determines whether it should be kept (indicated with values true/false)

It might seem like a hassle to separate things from each other, but combining simple parts with each other produces something big very quickly, similar to how doing repeated multiplication will very quickly produce a big number even when the individual numbers multiplied are small.

the shapes of those things don’t match up, so what happened there is that you did not consider the input and output of things, you bypassed it somehow. this has to be processed by you

read it.

does a word include words?

one thing does not include many things, no.
and you don’t need me to tell you that, it is clear when processing it. that’s what you’re missing, you were driving blindfolded.

1 Like

Ok i think i get where your coming from with ‘the shapes of those things don’t match up’

I processed in my head that ‘word’ would equal to the story just all split up, as I used .split.

Thank you , for your explanations you’ve been very helpful. And i feel i can finally move on now from this exercise.

I’m fairly sure that you’ll keep doing this for a while yet, because you’re still wired this way and it won’t go away immediately. It will though.

What you need to be doing is to observe the thing being done every step of the way. Nothing snaps into place and does the right thing by default, so you have to be there, putting things just the way you mean for them to be.

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.