Tell us what’s happening:
the code perfectly prints out the wanted output but the test failed
Your code so far
function chunkArrayInGroups(arr, size) {
//initializing the array that i will return
let testArr = [];
//initializing index that will be used to iterate over arr (original array)
let index = 0;
//initializing an array that will contain items from original array <arr> till it reaches limit <size> then it will be emptied to be refilled again.
let tempArr = [];
//iterate for <size-1> times to fill <tempArr> until limit <size> is reached so i will be resetted to repeat the for loop until we reach last Index <index> in original array <arr>
for (let i = 0; i < size && index < arr.length; i++, index++) {
// filling <tempArr> with original values <arr[index]>
tempArr.push(arr[index]);
console.log(`i: ${i} && index: ${index}`);
// if maximum size <size> for <tempArr> reached; fill <testArr> with values of <tempArr> and reset <tempArr> to an empty array and reset i=0
if (i === size - 1) {
//pushing <tempArr> into <testArr> so that <testArr[index]> contains sub array instead of concatination.
testArr.push(tempArr);
tempArr = [];
i = -1;
// console.log(`testArr: ${testArr} && tempArr: ${tempArr} && i: ${i}`);
console.log("testArr: ");
console.log(testArr);
console.log("tempArr: ");
console.log(tempArr);
console.log("i: " + i);
}
}
// console.log(`Array.isArray(testArr): ${Array.isArray(testArr)}`);
// console.log(testArr);
return testArr;
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
Output
i: 0 && index: 0
i: 1 && index: 1
testArr:
[ [ 'a', 'b' ] ]
tempArr:
[]
i: -1
i: 0 && index: 2
i: 1 && index: 3
testArr:
[ [ 'a', 'b' ], [ 'c', 'd' ] ]
tempArr:
[]
i: -1
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36
.
Challenge: Chunky Monkey
Link to the challenge: