Last active
September 25, 2017 22:22
-
-
Save dre4success/79bfe33ecb10bad20744ef1c87aea11f to your computer and use it in GitHub Desktop.
Write a function duplicate that takes in an array arr and an int n, and returns an array that duplicates arr n times
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
// with loop | |
function duplicate (arr, n) { | |
let arry = []; | |
for(let i = 0; i < n; i++) { | |
arry = [...arry, ...arr] | |
} | |
return arry | |
} | |
// without loop, recursion | |
function duplicate (arr, n) { | |
if(n == 1) { | |
return arr | |
} | |
return [...arr, ...duplicate(arr, n-1)] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment