-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8de69c
commit 9710963
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 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; | ||
} | ||
|