Write a function with parameters m,n to generate an array of m length, each item is n, no loop.
Just look at the first half, feel so easy but see can’t use the word loop, then use recursion. The following code
function creatAry(m, n) {
var ary = [];
function aryPush(m, n) {
if (ary.length == m)returnary; // Recursive end condition ary.push(n);returnaryPush(m, n); // It must be written herereturnOtherwise, the output is undefined}return (aryPush(m, n))
}
console.log(creatAry(3, 4));
Copy the code