Created
June 22, 2016 02:22
-
-
Save chrislpatton/be1334ba715ea7f69a708ba47fa46d14 to your computer and use it in GitHub Desktop.
Using JavaScript, given an array of n integers (example: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 112, 113] ), remove all odd numbers, leaving only the even numbers. Rules: NO LOOPING. This means native methods, or libraries that loop for you are not allowed either.
This file contains hidden or 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
| var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 112, 113] ; | |
| function onlyEvens(arr, index) { | |
| if (index === undefined) { | |
| index = 0; | |
| return onlyEvens(arr,index); | |
| } | |
| if (index === arr.length){ | |
| return arr; | |
| } | |
| if (arr[index] % 2 !== 0) { | |
| arr.splice(index,1); | |
| return onlyEvens(arr, index); | |
| } | |
| return onlyEvens(arr, index+1); | |
| } | |
| onlyEvens(nums); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment