This is the issue at hand: A function named filteredArray has been created. It takes two arguments - arr, a nested array, and elem. The purpose of this function is to return a new array by filtering out any nested arrays within arr that contain the specified elem. To achieve this, a for loop is utilized in the function.
The specific problem with the code: Upon examining my solution closely, I have noticed an interesting behavior. The function works as intended only when the nested loop starts with the number 3. If this position is changed (such as in the 2nd and 4th nested arrays), the function fails to produce the expected outcome.
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));