skip to Main Content

I am trying to be better in js and I stuck while trying to solve this problem,
"Using a for loop, to return a filtered version of the passed array such that any array nested within arr containing elem has been removed."

function filteredArray(arr, elem) {
  let newArr = [];
  // Only change code below this line
  for (let j=0;j<arr.length;j++){
    newArr.push(arr[j])
  }
  for(let i=0; i<newArr.length; i++){
    for (let k=0; k<newArr[i].length; k++){
      if (newArr[i][k] === elem){
        newArr.splice(i,1)
      }
    }
  }
  // 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 code above is what I have write by far. And I am getting an error;

for (let k=0; k<newArr[i].length; k++){

TypeError: Cannot read properties of undefined (reading ‘length’)
"

Can you guys help me, how can I fix the code?

Edit: Code should return []

4

Answers


  1. newArr.splice(i,1) here you are modyfing original array . and you need to modify nested array . try this -> newArr[i].splice(k,1)

    Login or Signup to reply.
  2. Here I am iterating over the newArr array in reverse order, and iterating over the nested arrays in reverse order as well. This ensures that removing elements from the array using the splice method will not cause the loop to skip over elements or access undefined values.

     function filteredArray(arr, elem) {
          let newArr = [];
          for (let j = 0; j < arr.length; j++) {
            newArr.push(arr[j]);
          }
          for (let i = 0; i < newArr.length; i++) {
            if (Array.isArray(newArr[i])) {
              for (let k = 0; k < newArr[i].length; k++) {
                if (newArr[i][k] === elem) {
                  newArr.splice(i, 1);
                  i--;
                  break;
                }
              }
            }
          }
          return newArr;
        }
        
        console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)); // should return []
    
    Login or Signup to reply.
  3. You should be looking to take advantage of functions provided by the JavaScript interpreter and high level functions where possible. There are many such functions that operate on collections such as Arrays.

    One such function: Array.prototype.filter.

    Filter on two conditions:

    1. Test if the element of arr is an Array (i.e. Array.prototype.isArray)
    2. Test if the element is equal to elem (i.e. Array.prototype.includes – this answer assumes elem is a primitive (see note below))
    let filteredArray = (arr, elem) => {
      let result = []
      for ( inner of arr ) {
        if ( !( Array.isArray(inner) && inner.includes(elem)) )
          result.push(inner)
      }
      return result
    }
    
    console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9] ], 3));

    Note: Array.prototype.includes tests using equality and Objects are references so their "value" is not equal to a primitive’s value. example:

    let x = [ 1 ]
    let y = [ 1 ]
    let val = [ [ 1 ], y ]
    console.log(val.includes(x))
    console.log(val.includes(y))
    Login or Signup to reply.
    1. Since you want to call the Array.prototype.splice function, you MUST loop in the backward direction.
    2. Secondly, when the number is found, break the inner loop.
    function filteredArray(arr, elem) {
      let newArr = [];
      // Only change code below this line
      for (let j=0;j<arr.length;j++){
        newArr.push(arr[j])
      }
      
      for(let i = newArr.length - 1; i >= 0; i--){
        for (let k=0;  k < newArr[i].length; k++){
          if (newArr[i][k] === elem){
            newArr.splice(i,1);
            break;
          }
        }
        
    
      }
      // Only change code above this line
      return newArr;
    }
    
    console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search