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));
8
  • please add the wanted result. Commented Feb 23, 2020 at 10:03
  • 1
    What is row? ? Commented Feb 23, 2020 at 10:03
  • @CertainPerformance I just editd my code. Please check now. Commented Feb 23, 2020 at 10:04
  • for(var i=0; i<row.length; i++) What is row? Commented Feb 23, 2020 at 10:04
  • What if the input is [1,2,5,7,8,12]? What would be the output then? (If you always want the same result, there is no need for a function, then you just hard-code the result). Commented Feb 23, 2020 at 10:05

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.