When you look at the problem statement it says that as soon as you find the âelemâ value inside the given nested arrays. You need to remove any arrays that contains this âelemâ.
When you are stuck, the fastest way to get Hints would be to use the âGet Helpâ button â âGet a Hintâ button (This buttons are located below the âRun Testsâ button), which will lead you here for this particular challenge:
function filteredArray(arr, elem) {
let newArr = ;
// Only change code below this line
for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf(elem) === -1) {
// arr.splice(i, 1);
newArr.push(arr[i]);
You are putting the element in the answer then removing it if it matches, almost like if you were buying apples and you put the apple in cart and then looked at the apple and if it was bad you took out. Why not only put the apple (element) in the cart (array) if it is good (doesnât have elem).
Also, you update the loop variable âiâ in the âforâ statement, if you do that again you will skip every other entry in arr.
Note:
One skill that is good to practice is to be able to picture what is happening at each step in your head. When I was first learning I would write out the data and steps on a piece of paper and figure the state of all the variables at each step of the process, if you do that then over time it will come more easily to the point of being able to do it in your head.
I was replying to the initial post, I see further down where you check before pushing and that is what I meant. As in:
function filteredArray(arr, elem) {
let newArr = ;
// Only change code below this line
for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf(elem) === -1) {
newArr.push(arr[i]);
}
}
// Only change code above this line
return newArr;
}
You should always do a strict equality check unless you want/need type coercion. If you do not want/need the type to be implicitly coerced, donât use ==.
indexOf will return -1 if it does not find the element and the code just checks if the return is -1.
There is no need to use == here and when that is the case you should always use === to avoid any potential unexpected type coercion.
Using === can also help beginners avoid the mistake of using = for an equality check, simply because when you have typed === enough times, and that is mainly all you use, you wonât as easily make that mistake. I think itâs both muscle memory and it also gives a better visual distinction between the two.
Thank you Silgarth for explaining this - I was originally doing it âbackwardsâ by finding the (sub)arrays with the elem, instead of focussing on the ones that didnât have it, which is a more efficient way to do it.