How to fill a range of fresh



  • 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]]
    


  • Actually, you don't need a class. At all. Yeah, someone might say that the tuition is quite over the cycles. But he won't be honest with you.

    Addressing the challenges in JavaScript (as in many other languages) involves creating a set of challenges. With too much of the view (in your case, too many elements) there may be a recuperation of the steak (StackOverflow) The situation could correct the optimization https://ru.wikipedia.org/wiki/%D0%A5%D0%B2%D0%BE%D1%81%D1%82%D0%BE%D0%B2%D0%B0%D1%8F_%D1%80%D0%B5%D0%BA%D1%83%D1%80%D1%81%D0%B8%D1%8F which is part of ES6, but none of the main media of JS implementation https://kangax.github.io/compat-table/es6/#test-proper_tail_calls_%28tail_call_optimisation%29 ♪

    When you use cycles, you won't have any problems like this.


    I'll give you a beautiful way to accomplish your specific task with the language that is built:

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

    This function can be used to clearly follow the objective:

    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]));




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2