am93
April 12, 2021, 4:31pm
1
Hey guys, I feel as though this solution could work but it’s not, can you guide me to the right direction?
TypeError is: Cannot read property ‘apply’ of undefined
**Your code so far**
// set arguments in function regardless of how many?
function destroyer(arr, ...args) {
// concat and filter through arr to remove what is included in arguments
return arr
.concat(...args)
.filter(item => item.includes(...args));
}
console.log(destroyer(["tree", "hamburger", 53], "tree", 53));
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36
.
Challenge: Seek and Destroy
Link to the challenge:
I feel like this is backwards. You are checking if a single array item contains all of the other destroyer args?
am93
April 12, 2021, 4:38pm
3
Ah I guess not all but any destroyer args correct? I’m not sure how else to create that since some have 2 args some 3 etc
Unless I should loop through ...args
and then run filter on arr
using ...args[i]
?
ILM
April 12, 2021, 4:42pm
4
an other issue is that includes
accept one single parameter, using ...args
only the first item is passed in
also, is item
an array?
and why you are including the elements in args
inside the arr
array?
Do you want all of the args included in the item or the item included in all of the args?
am93
April 12, 2021, 4:46pm
6
If you mean want as in want to filter out then I want to filter out the args in the array and return the array.
am93
April 12, 2021, 4:47pm
7
Ok I guess I’d rather do one issue at a time though because I believe I now have 4 questions in front of me
You should use includes
as someList.includes(thisThing)
I know you hate MDN, but it’s useful to look up how the function works somewhere
Array.prototype.includes() - JavaScript | MDN
JavaScript Array includes() Method
ILM
April 12, 2021, 4:50pm
9
ignore the last one, all the others are for this line
trying to understand what you want to do with it and what you think it is doing
am93
April 12, 2021, 4:55pm
10
I do still check MDN actually. I used this for the previous problem and it worked I’m not sure why it may not here.
For example I thought it’d check to see if “tree” (from arr) included “tree” (from args) then if so filter it out.
Right, but that’s backwards of how the documentation says the includes
function works. includes
checks if
let thisList = ['a', 'b', 'c'];
includes
let thisThing = 'd';
via
console.log(thisList.includes(thisThing));
am93
April 12, 2021, 4:58pm
12
I don’t know why I’m not seeing how. Wouldn’t it be filtered out if it returns true?
const array1 = [1, 2, 3];
console.log(array1.includes(2));
// expected output: true
When I look at this: .filter(item => item.includes(...args));
I read filter item where item includes args
am93:
item.includes(...args)
item
is one thing
...args
is many things
The semantics is many.includes(one)
am93
April 12, 2021, 5:01pm
14
Right yeah I see that issue but does that mean by nature I can’t use includes at all or that I can tweak something and use includes still?
Earlier I mentioned if this could work
Welp I tried this but it’s not working because it says item.includes is not a function
let newArgs = [...args];
for (let i = 0; i < newArgs.length; i++) {
arr.filter(item => item.includes(newArgs[i]))
}
ILM
April 12, 2021, 5:18pm
15
you can use includes
but you need to figure out how, like, what array you want to use it on?
am93
April 12, 2021, 5:27pm
16
Doesn’t filter go through each item in arr? Unfortunately I’m not understanding this
ILM
April 12, 2021, 5:28pm
17
yes, so item
is not an array, and includes
can be used only on an array
am93:
item.includes(...args)
what’s the array here?
am93
April 12, 2021, 5:30pm
18
arr but when I try that it doesn’t filter correctly
ILM
April 12, 2021, 5:32pm
19
but arr
is not here:
am93:
item.includes(...args)
and you want to check if the single item is to remove or not (equals one of the elements in args
)
am93
April 12, 2021, 5:35pm
20
ILM:
but arr
is not here:
What do you mean?
So for this I’m saying I tried the loop here