0

The problem is as follows : We have defined a function, filteredArray, which takes arr, a nested array, and elem as arguments, and returns a new array. elem represents an element that may or may not be present on one or more of the arrays nested within arr. Modify the function, 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.

The problem I am having with my code : In my solution specifically , I don't understand why the code only works as expected if the nested loop starts with the number 3 . If that number's position is changed -as in the 2nd and 4th nested arrays- the function doesn't work as expected .

function filteredArray(arr, elem) {
  let newArr = [];
  let myNestedArray;
  // Only change code below this line
  for (let i = 0; i < arr.length; i++) {
    myNestedArray = arr[i];
    for (let j = 0; j < myNestedArray.length; j++) {
      if (myNestedArray[j] === elem) {
        console.log(`Main arr is => ${arr}`);
        console.log(`Nested array is =>  [${myNestedArray}]`);
        console.log(`Elem is => ${elem}`);
        arr.splice(arr.indexOf(myNestedArray), 1);
        myNestedArray = [];
        console.log(`#######`);
    }
}
}
newArr.push(arr);
  // Only change code above this line
  return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
Mohammed
  • 45
  • 6
  • 1
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? – David May 05 '23 at 12:09
  • *"I don't understand why the code only works as expected if the nested loop starts with the number 3."* that's not correct, put the first triplet `[3, 2, 3]` at the end of the array and your code will filter other elements. Or try putting the `3` at the start in all sub arrays and check the result. – Thomas May 05 '23 at 12:56

3 Answers3

0
  • Create a flag called isValueFoundInSubArray
  • When you're at the end of a subArray, add it to your new array if this flag is false

Code :

  function filteredArray(arr, elem) {
    let newArr = [];
    let myNestedArray;
    // Only change code below this line
    for (let i = 0; i < arr.length; i++) {
      myNestedArray = arr[i];
      let isValueFoundInSubArray = false;
      for (let j = 0; j < myNestedArray.length; j++) {
        if ((!isValueFoundInSubArray) && myNestedArray[j] === elem) {
            isValueFoundInSubArray = true;
        }
        if ((j === myNestedArray.length - 1) && !isValueFoundInSubArray) {
         newArr.push(myNestedArray)
        }
      }
    }
    // Only change code above this line
    return newArr;
  }
l -_- l
  • 618
  • 4
  • 15
0

When you remove a house from the presentation, what happens is that the circle does not go to the last house that you are considering and as a result, the work that you are considering cannot be done. It is better to use foreach

function filteredArray(arr, elem) {
  const newArr = [];
  // Only change code below this line
  arr.forEach((myNestedArray) => {
    myNestedArray.forEach((i) => {
      if(i===elem){
        const loc = myNestedArray.indexOf(i)
        myNestedArray.splice(loc,loc+1)
        console.log(`Main arr is => ${arr}`);
        console.log(`Nested array is =>  [${myNestedArray}]`);
        console.log(`Elem is => ${elem}`);
        console.log(`#######`);
      }
      if(!myNestedArray.includes(elem)){
        return ;
      }
    });
    arr.push(myNestedArray);
    arr.shift();
  });
  newArr.push(arr);
  // Only change code above this line
  return newArr;
}

console.log(
  filteredArray(
    [
      [3, 2, 3],
      [1, 6, 3],
      [3, 13, 26],
      [19, 3, 9],
    ],
    3
  )
);
0

Here is an example of removing the data from a nested array in JavaScript by using the Splice method which will help by knowing the index of the nested element.

Code:

let mainArray= [[1, 2], [3, 4, 5], [6, 7, 8, 9]];
let nestedIndex= 3;

mainArray.forEach((array, index) => {
  let nestedIndex= array.indexOf(nestedIndex);
  if (nestedIndex!== -1) {
    array.splice(nestedIndex, 1);
  }
});