Tell us what’s happening:
Can someone please tell me what I am doing wrong here?
I tried two different solutions and both failed…
in the first one I used 1 “for loop” and used one condition statment to check if “elem” is not in the nested array. However that solution failed for some reason so I tried to use two “for loops” instead which also failed… I already took a look at the solution but I am just curious to know why is my answer failing… You can see both solutions down below:
**My code so far**
My first solution:
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (let i = 0; i < arr.lenght; i++) {
if (!elem in arr[i]) {
newArr.push(arr[i]);
}
}
// Only change code above this line
return newArr;
}
let randomArray = [[1, 2, 1], [1, 6, 3], [3, 13, 26], [19, 2, 9]];
console.log(filteredArray(randomArray, 3));
My second solution:
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (let i = 0; i < arr.lenght; i++) {
let isIn = false;
for (let j = 0; j < arr.lenght; j++) {
if (arr[i][j] === elem) {
isIn = true;
}
}
if (!isIn){
newArr.push(arr[i]);
}
}
// Only change code above this line
return newArr;
}
let randomArray = [[1, 2, 1], [1, 6, 3], [3, 13, 26], [19, 2, 9]];
console.log(filteredArray(randomArray, 3));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36
Challenge: Basic Data Structures - Iterate Through All an Array’s Items Using For Loops
Link to the challenge: