odin-codespace/javascript/recursion/q9.js

21 lines
543 B
JavaScript
Raw Permalink Normal View History

2024-01-24 07:34:04 -05:00
function replicate(multiple, number) {
if (multiple <= 0) return [];
// let arr = [];
// original and too long
// if (multiple >= 1) {
// arr.push(number);
// } else {
// arr.push(replicate(multiple - 1), number);
// }
// const concated = arr.concat(replicate(multiple - 1, number));
// return concated;
return [number].concat(replicate(multiple - 1, number));
}
console.log(replicate(3, 5)); // [5, 5, 5]
console.log(replicate(1, 69)); // [69]
console.log(replicate(-2, 6)); // []
console.log(replicate(60, 30));