Hello!
I’ve been working through the Javascript Exercises and this one in particular completely stumped me. Even though the ReadMe file explicitly links to a page where the general concepts are explained, and I read this page (several times), I still was completely lost and had to view the solution.
A quick aside: I’m not sure if it’s just me but I feel at this point in the course there has been a big step up in difficulty and I don’t feel entirely prepared for it. I do all of the readings, but it’s difficult without more exercises to balance it out. I can read about something a million times and until I actually apply it in a meaningful way, the information is pretty close to useless (for me).
I found a lot of great resources for “args…” and array.filter but aside from cycling through some basic numbers, I couldn’t really find anything that made sense to me, personally, by applying it to something meaningful.
Here is the answer provided by the official solutions repo:
var removeFromArray = function(…args) {
const array = args[0]
return array.filter(val => !args.includes(val))
}
I was beginning to get the first line by the time I broke down and looked up the solution. It wasn’t clear to me in the documentation whether or not I needed to add an additional argument representing something else, or at least some brackets [] to denote the array.
The final line is also what gets me - I don’t understand “val” and another user pointed this out in this thread: Remove from Array
The above thread confirms that “val” is just a name for the elements of the array. Could in theory “val” be anything? Could I have called it ‘elementsOfArray’ or something? I think a few lessons ago we learned about naming variables in ways that make sense… hmmm
I guess one of the parts that stumps me the most is the difference between “…args” and “val”. I mean, I know they’re different, but-- is “args…” a variable for the entire argument as a whole and then ‘val’ is a variable for just the items inside of the array?
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
It looks to me like that function is - is passing the right word? - six arguments.
So the array.filter
filters through the elements of the array.
(I wish there were some arrow function exercises before introducing(?) them here, I guess).
Val is the elements of the array, so the function parameters.
The expression of the function is, I think, telling the function to return arguments not included in the “second half” of the above argument, being the “3, 2” number?
I’m not even sure of that but it would be my best guess.
If you guys know of any material that can go side-by-side with The Odin Project, I’d really appreciate it! I feel like this course is not enough (which of course I knew going into it, but I thought I could at least complete it and do other tutorials after, but since this is giving me so much trouble, I’d definitely be interested in some companion materials!)
Thanks! And apologies for the long post, I am an English major after all