I'm on FCC challenge JavaScript Algorithms and Data Structures Basic Algorithm Scripting
I was expecting the following codes return same result, but just the 2nd one is passing the FCC challenge, and I'd like to understand why.
Code 1:
function chunkArrayInGroups(arr, size) {
let newArr = [];
for (let i = 0; i < (arr.length); i + size) {
console.log(`arr = ${arr}`)
let subArr = []
subArr.push(arr.splice(i, i + size))
newArr.push([subArr])
}
return newArr
}
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4));
Code 2
function chunkArrayInGroups(arr, size) {
let newArr = [];
for (let i = 0; i < (arr.length); i + size) {
newArr.push(arr.splice(i, i + size))
}
return newArr
}
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4));
Could someone please help me understand why the different results?