Skip to content

Instantly share code, notes, and snippets.

@chrislpatton
Created June 22, 2016 02:22
Show Gist options
  • Save chrislpatton/be1334ba715ea7f69a708ba47fa46d14 to your computer and use it in GitHub Desktop.
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.
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