8000 [ADD] Filtered array added · luisprooc/js-algorithms@a2bfbe1 · GitHub
[go: up one dir, main page]

Skip to content

Commit a2bfbe1

Browse files
committed
[ADD] Filtered array added
1 parent 7826341 commit a2bfbe1

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,20 @@ rangeOfNumbers(6, 9) should return [6, 7, 8, 9].
239239
rangeOfNumbers(4, 4) should return [4].
240240
```
241241

242+
243+
## Filtered Array
244+
245+
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.
246+
247+
248+
```javascript
249+
filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18) should return [[10, 8, 3], [14, 6, 23]]
250+
251+
filteredArray([["trumpets", 2], ["flutes", 4], ["saxophones", 2]], 2) should return [["flutes", 4]]
252+
253+
filteredArray([["amy", "beth", "sam"], ["dave", "sean", "peter"]], "peter") should return [["amy", "beth", "sam"]]
254+
255+
filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3) should return []
256+
257+
The filteredArray function should utilize a for loop
258+
```

src/filteredArray.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function filteredArray(arr, elem) {
2+
let newArr = [];
3+
for(let i = 0; i < arr.length; ++i){
4+
if(arr[i].indexOf(elem) === -1){
5+
newArr.push(arr[i])
6+
}
7+
}
8+
return newArr;
9+
}
10+
11+
console.log(filteredArray([["trumpets", 2], ["flutes", 4], ["saxophones", 2]], 2));

0 commit comments

Comments
 (0)
0