-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListFiltering.js
More file actions
36 lines (35 loc) · 961 Bytes
/
ListFiltering.js
File metadata and controls
36 lines (35 loc) · 961 Bytes
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
35
/**
* In this kata you will create
* a function that takes a list of non-negative integers
* and strings and returns a new list
* with the strings filtered out.
*
* Example
* filter_list([1,2,'a','b']) == [1,2]
* filter_list([1,'a','b',0,15]) == [1,0,15]
* filter_list([1,2,'aasf','1','123',123]) == [1,2,123]
**/
/**
* without array's filter() method
* traditional way of looping arrays,
* faster than filter() method
**/
export default function filter_list_old(l) {
// Return a new array with the strings filtered out
let filteredList = [];
if (Array.isArray(l)) {
for (let i = 0; i < l.length; i++) {
if (typeof l[i] === 'number') {
filteredList.push(l[i]);
}
}
return filteredList;
}
}
/**
* using filter() method to return only number list
**/
function filter_list_es6(l) {
// Return a new array with the strings filtered out
return Array.isArray(l) && l.filter((val)=> (typeof val === 'number')) || undefined;
}