0

I am facing a difficulty to print arrays when I call makeArrays() function. Can anyone help me out please?

var arr =[10,11,12,13];

    var arr2= [];
    for(var i=0; i<arr.length; i++){
        if(i>3){
          return arr2[i];
        } else {
            i= i+1;
           arr2.push(arr.slice(0,4));
        }
    }


console.log(makeArrays(arr));
loveCoding
  • 105
  • 8

1 Answers1

0

I think you do not need the condition to get the expected result here. Simply push the sliced array in each iteration.

You can try the following way:

var arr =[1,2,3,4];

function makeArrays(arr) {
  var arr2= [];
  for(var i=0; i<arr.length; i++){ 
    arr2.push(arr.slice());
  }
  return arr2;
}

console.log(makeArrays(arr));
loveCoding
  • 105
  • 8
Mamun
  • 66,969
  • 9
  • 47
  • 59