-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilteredArray
34 lines (32 loc) · 1.04 KB
/
filteredArray
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*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.
*/
function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (let i = 0; i < arr.length; i++) {
var found = false;
if (Array.isArray(arr[i])){
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j] == elem){
found = true;
break;
}
}
}
else if (arr[i] == elem){
found = true;
break;
}
if (!found)
{ console.log(found);
newArr.push(arr[i]);}
}
// 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));