arr=[] function recurArray(startIndex,endindex,data){ if(startIndex==endindex){ return arr; }else{ startIndex++; arr.push(data[startIndex]); recurArray(startIndex,endindex); } } recurArray(0,10,[1,2,3]) console.log(arr)// выдает Cannot read property '2' of undefined //а должно быть [[1,2,3],[1,2,3]......[1,2,3]] 

Closed due to the fact that off-topic participants D-side , VenZell , Nick Volynkin Jun 8 '16 at 0:52 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - D-side, Nick Volynkin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Alas, recursion is not needed here. Use loops. - Dmitriy Simushev
  • @DmitriySimushev, they are interchangeable in principle :) - Grundy
  • one
    @Grundy, only in principle. A recursive call may well overflow the stack. Well, to make a crutch where it is not necessary - bad practice - Dmitriy Simushev
  • 2
    @DmitriySimushev is not in practice , but it does not seem like a practical task. Although the warning really does not hurt. - D-side
  • @Knopkatyk, in practice - this problem is solved through a cycle. And if artificial restrictions are significant (laboratory, exam, interview), then it is worth distinguishing a part of the question “essentially” from the question. This is now a remote debugging session that reveals the missing argument. - Dmitriy Simushev

3 answers 3

In fact, you do not need recursion. Totally. Yes, someone might say that recursion completely replaces cycles. But he will not be honest with you.

Implementing recursive calls in JavaScript (as in many other languages) involves creating a call stack. If the recursion depth is too large (in your case there are too many elements) a stack overflow can occur ( the same StackOverflow ). The situation could be corrected by tail recursion optimization, which is part of ES6, but none of the main JS runtimes support it .

When using cycles, you will not have such problems.


I will give a beautiful way to solve your specific problem with built-in language tools:

 var arr = (new Array(10)).fill([1,2,3]); console.log(arr); 

And to clearly follow the condition of the problem, you can use the following function:

 var fillArray = function(startIndex, endIndex, value) { var res = []; for (var i = startIndex; i <= endIndex; i++) { res[i] = value; } return res; } console.log(fillArray(0, 10, [1, 2, 3])); 
  • Where is the answer to the question here? And the proposed code fills the entire array, not the values ​​from startIndex to endindex . What minus set? - Qwertiy
  • @Qwertiy, (new Array(10)) creates an array with a capacity of 10 (equivalent to endIndex - startIndex ). Argument startIndex - I consider it a crutch, necessary for the construction of a recurrent filler. Well, my answer comes down not to a piece of copy-paste, but to try to convince the TS that recursion is not needed. As for the minuses - I calmly relate to them. Especially to inadequate. - Dmitriy Simushev
  • Convince someone who does homework for recursion that recursion is not needed? Well, yes, it is logical. - Qwertiy
  • @Qwertiy, at the same time updated the answer, citing the implementation of the function from the condition on the cycles. After all, an answer without a copapasta is meaningless, isn't it? - Dmitriy Simushev pm
  • @Qwertiy, I answer not only and not so much the question of the vehicle (it comes down to remote debugging and searching for the missing argument). My answer is addressed to those who are really interested in how recursion relates to cycles. - Dmitriy Simushev

In the recurArray(startIndex,endindex) call, the data parameter is not passed, so it is undefined inside the call

In addition, judging by the expected result in the string

 arr.push(data[startIndex]); 

need to be replaced by

 arr.push(data); 

And it is worth removing the dependence on the external variable:

 function recurArray(startIndex, endindex, data) { if (startIndex == endindex) { return [data]; } else { return [data].concat(recurArray(startIndex + 1, endindex, data)); } } arr = recurArray(0, 10, [1, 2, 3]); document.write(JSON.stringify(arr)); 

     var arr=[]; function recurArray(startIndex,endindex,data){ if(startIndex==endindex){ return arr; }else{ startIndex++; arr.push(data); recurArray(startIndex,endindex,data); } } recurArray(0,10,[1,2,3]) document.write(JSON.stringify(arr))