I’m pretty sure you don’t need the else continue. (the loop continues anyway).
But more important, your for loop dies when you break out of it. So it ends when you find the elem for the first time. The elem can occur in multiple arrays, and you have to remove them all.
Your structure of the return value is nested nested array. Try making it nested array. Also, you are supposed apply the filter for each nested array. Right now your filter only applies to the first nested array that meets the if condition.
How’s the return value nested nested?
And yes, I am stopping it after one but the code doesn’t clear the test case where the number is not repeated either.Like this one: [[10, 8, 3], [14, 6, 23], [3, 18, 6]]
Here the element we’re supposed to delete the container of is 18. And 18 appears just once so my code should still work. Please help
arr.splice() is applied to the arr which is the nested array argument. So, its return value is also a nested array.
Suppose the arr after the splice looks like [ [1, 2], [3, 4] ], then ...arr results [1,2] [3,4]. Since you are putting that to another array, [...arr] is [ [1, 2], [3, 4] ]; this is what you are pushing to newArr.
newArr then becomes [ [ [1, 2], [3, 4] ] ] , which is nested nested array.
I think I misunderstood the spread operator.
If arr=[1, 2, 3, 4]; And if I were to add that to another array, anotherArr. anotherArr=arr
Can I do that? It seems to work well on the chrome console.
Or this: anotherArr.push(arr);
@camperextraordinaire hey that idea of using push worked out great, thanks! @graven_whismas try flipping your test case of the if statement. remember you want to push in the items from the array that do not have the elem inside them.
This worked and passed.
for (let r=0; r < arr.length; r++) {
if (arr[r].indexOf(elem) < 0) {
newArr.push(arr[r]);
}
}