Skip to content

Instantly share code, notes, and snippets.

@dre4success
Last active September 25, 2017 22:22
Show Gist options
  • Save dre4success/79bfe33ecb10bad20744ef1c87aea11f to your computer and use it in GitHub Desktop.
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
// 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