1

There is a problem in FreeCodeCamp.I'm not here to search solution to that problem. While trying to solve the problem I found some lines of my code do not work. I can not understand why that is not working. So I am here to ask YOU, good people, to help me.

problem

There is a function. I will pass an array and a number to that function. And what I need to return is also an array. The array is a multidimensional array.

what I want to do

First of all I want to check if the inner or subarray contains the number i passed while calling the function. If that contains i need the index of that number in that subarray. Then I want to delete the number from that subarray using splice(). At last I wanted to return an array where there are sub arrays in it but none of them contain the given number.

where i am stuck in

But I am stuck in finding the index of the number in sub arrays, how can i use splice() to delete the number? Is it possible to do this way? Do you have any better suggestion for me?

my code

where for the first for loop it just prints -1 for the second loop, it prints the index of the array,not the index of the subarray.

function filteredArray(arr, elem) {
    let newArr = [];
    // Only change code below this line
    newArr = [...arr]
    let L = arr.length;
    for (let i = 0; i < L; i++) {
        //  -------1-----------
        for (elem in newArr[i]) {
            console.log(newArr[i].indexOf(elem));
        }
    }
    console.log('first loop ends')
    for (let i = 0; i < L; i++) {
        //  --------2---------
        for (let j = 0; j < newArr[i].length; j++) {
            if (newArr[i][j] == elem) {
                console.log(newArr[i].indexOf(elem))
            }
        }

        // Only change code above this line
        return newArr;
    }
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

function filteredArray(arr, elem) {
    let newArr = [];
    // Only change code below this line
    newArr = [...arr]
    let L = arr.length;
    // for (let i = 0; i < L; i++) {
    //     //  -------1-----------
    //     for (elem in newArr[i]) {
    //         console.log(newArr[i].indexOf(elem));
    //     }
    // }
    console.log('first loop ends')
    for (let i = 0; i < L; i++) {
        //  --------2---------
        for (let j = 0; j < newArr[i].length; j++) {
            if (newArr[i][j] == elem) {
                console.log(newArr[i].indexOf(elem))
            }
        }

        // Only change code above this line
        return newArr;
    }
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
  • The main problem here is that you're only logging stuff but never pushing anything to `newArr`. You have also moved `return newArr;` inside your 2nd for loop, which means the function ends after the first iteration. –  Jun 03 '22 at 09:02
  • In your first loop, try with `for (elem of newArr[i])` (note `of` vs `in`). This way `elem` will be the element of `newArr[i]`, rather than its index. However, you then have the problem of `indexOf(elem)` selecting only the *first* index of the element, and it'll not find both 3s in your sub-array `[3, 2, 3]`. For using splice, see https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array – BryceCicada Jun 03 '22 at 09:06
  • Also, a word of caution... your array copy with the spread operator at the top `newArr = [...arr]` will copy the outer array, but the inner sub-arrays will reference the same sub-arrays as in the input `arr`. Any splicing you do on the sub-arrays of `newArr` will mutate the same sub-arrays of the input `arr`. The outer array copy at the top implies this might not be what you want. – BryceCicada Jun 03 '22 at 09:13

1 Answers1

0

When removing values from an array you don't want to use a mutating method like .splice() -- the original array will change. If .splice() removes a number, the length of the array decreases and all indexes at and after the index of the removed number will shift (unless you replace that number instead). The non-mutating methods such as filter() and .map() makes a copy of the array and returns the copy leaving the original array intact. See this article for a easy reference of what mutates and what doesn't.

You could simplify the process of removing a given number from an array of arrays by using .map() on each sub-array and .filter() each sub-array with the condition of returning only numbers that do not equal the given number.

const data = [[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]];

const filterCols = (target, arrArr) => 
  arrArr.map(sub => sub.filter(num => num !== target));

console.log(filterCols(3, data));
zer00ne
  • 41,936
  • 6
  • 41
  • 68