Last active
March 14, 2017 21:32
-
-
Save jennyvallon/2718a7413f59e1b96264af00f69edf65 to your computer and use it in GitHub Desktop.
Code test
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
var returnEvensOnly = function(array,counter,result) { | |
if (counter===undefined){counter=0;} | |
if (result===undefined){result=[];} | |
if (counter <= array.length) { | |
if(array[counter]%2===0){result.push(array[counter]);} | |
return returnEvensOnly(array,counter+1,result); | |
} else { | |
return result; | |
} | |
}; | |
returnEvensOnly(array,counter,result); | |
If I wanted to perform all operations on and return the same exact array I would do the following instead: | |
var returnEvensOnly = function(array, counter) { | |
if (counter===undefined){counter=0;} | |
if (counter <array.length) { | |
if(array[counter]%2===0){ return returnEvensOnly(array,counter+1);} //If i get a remainder | |
else{ | |
array.splice(counter,1); | |
return returnEvensOnly(array,counter); | |
} | |
} else { | |
return array; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment