1

I need to slice and array using a for loop and the slice() and push() methods.

I tried every possible way (except the right one obviously) but no way to figure out the solution.

Please to explain because I am always in trouble when it comes to recursion and for loops.

 function chunkArrayInGroups(arr, size) {
  // Break it up.
  var finalArray = [];

  // var firsPart = arr.slice(0, 2);
  // var secondPart = arr.slice(2, 4);
  // console.log(firsPart);
  // console.log(secondPart);

  // finalArray.push(firsPart, secondPart);

  for (var i = 0; i < arr.length; i++) {
    var slicingIndex = 0;
    var sliced = arr.slice(size);

    finalArray.push(sliced);
    }

  return finalArray;
}

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2));

Thanks in advance for your help!

0

2 Answers 2

1

You could use a while loop and slice with the index and the updated index with the wanted length.

function chunkArrayInGroups(arr, size) {
    var finalArray = [],
        i = 0;

    while (i < arr.length) {
        finalArray.push(arr.slice(i, i += size));
    }
    return finalArray;
}

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2));
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

By no means an expert, but I think this might help.

function chunkArrayInGroups(arr, size) {
  var finalArray = [];

  for (var i = 0; i < size; i++) {
    if (finalArray.length !== size) {
      var sliced = arr.slice(i);
      finalArray.push(sliced);
    }
  }

  return finalArray;
}

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.