function chunkArrayInGroups(arr, size) {
if(size <= 0) return arr;
var newArr = [];
//Loop through the array and slice it from i to i + size,
//Increment i in the slice argument.
for(var i=0; i < arr.length;) {
newArr.push(arr.slice(i, i+=size));
}
return newArr;
}
Is it ok to increment in an argument like this? Or is it bad practice? How can I test this for speed? Thanks in advance
This is my solution
function chunkArrayInGroups(arr, size) {
var newArr = [];
for(var i=0;i<arr.length ;i += size){
var arr1 = arr.slice(i,i+size);
newArr.push(arr1);
I find it frustrating that I was unable to solve this challenge. I finally gave up and looked online for a solution. It turns out I was on the right track, but my math was a little off. For some reason I really struggle with arrays, but I find most of the other challenges to be easy.
Thank you for sharing your solution. I came up with pretty much the same one except I didnāt incorporate the stop variable. I could figure why my code wouldnāt pass. I guess I didnāt understand how the slice method worked. Thanks again.
Can someone please explain in regards to the basic code solution, why using if (a % size !== size - 1) is a good solution? Just a little confused as Iāve never seen a modulus used in an if statement.
I spent 2 hours on this, but came out with an intermediate solution. Im proud of my self function chunkArrayInGroups(arr, size) {
// Break it up.
arr1= [];
for (i=0; i<arr.length/size; i++){
arr1.push(arr.slice(sizei, size(i+1)));
} return arr1; //console.log(arr.slice(sizei, size(i+1))); //console.log(arr.shift(1));
}
chunkArrayInGroups([āaā, ābā, ācā, ādā], 2);
I came up with this. I would greatly appreciate feedback.
function chunkArrayInGroups(arr, size) {
arrayContainer = [];
for (i = 0; i < arr.length; i += size){
if (i === 0 || i % size === 0) {
arrayContainer.push(arr.slice(0 + i, i + size));
}
}
return (arrayContainer);
}
My solution. Any feedback on how good or bad it is are much appreciated.
function chunkArrayInGroups(arr, size) {
var topArray = [];
i = 0;
var begin = 0;
var end = size;
while (i < arr.length/size) {
topArray.push(arr.slice(begin,end));
i++;
begin += size;
end += size;
}
return topArray;
}
function chunkArrayInGroups(arr, size) {
var arr1 = ;
var cal = Math.ceil(arr.length / size);
// calculate the No of subarrays needed using this equation.
for(var i = 0; i < cal; i++){
arr1.push(arr.splice(0, size));
//using splice as it change the original array so every time for loop will
//start from the last position it ends at the time before.
}
return arr1;
}
Just wanted to reiterate what @luishendrix92 had mentioned at the beginning of this thread. It seems that many of the solutions posted, (including the ones in the Wiki) will produce an infinite loop when the size passed is 0 or less!
I believe it would be prudent for everyone to check their code against this scenario, because I donāt think anyone wants a piece of code causing this issue.
Perhaps this scenario should be addressed in the Wiki and the exercise itself?
Thanks @luishendrix92! I certainly wouldnāt have caught this problem myself!
function chunkArrayInGroups(arr, size) {
//Index calculates numbers of subarrays in newArr
let newArr = [];
let index = arr.length/size;
//Slice chunks old array into new
for (let i =0; i<index; i++){
newArr[i] = arr.slice(i*size,(i+1)*size);
}
return newArr;
}
chunkArrayInGroups(["a", "b", "c", "d", "e", "f"], 2);
brief explanation:
If you know the array is 6 units long, stored in 2 array chunks, that means you have 3 subarrays.