Question: 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.
-
filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)should return[ [10, 8, 3], [14, 6, 23] ] -
filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)should return[ ["flutes", 4] ] -
filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")should return[ ["amy", "beth", "sam"] ] -
filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)should return[ ] -
The
filteredArrayfunction should utilize aforloop
I don’t really know where I am going wrong…
Console output:
// running tests
arr[i] is undefined
arr[i] is undefined
arr[i] is undefined
arr[i] is undefined
arr[i] is undefined
// tests completed
function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for(let i = 0 ; i < arr.length ; i++)
{
for(let j = 0 ; j < arr[i].length ; j++)
{
var x = arr[i][j];
if(x === elem)
{
arr.splice(i , 1);
}
}
}
newArr = [...arr];
// change code above this line
return newArr;
}
// change code here to test different cases:
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-all-an-arrays-items-using-for-loops