Pushing elements instead multi-d array

Tell us what’s happening:
My newArr variable is returning all the elements of subArr in sequence, instead of returning a 2d array with all subArr arrays.

Could someone please help me find where I’m mistaking?

Thanks in advance,
Luciano

Your code so far

function chunkArrayInGroups(arr, size) { 
  console.log(`------------------`)
  console.log(`arr = ${arr}`)
  console.log(`size = ${size}`)
  console.log(`arr.length = ${arr.length}`)
  let newArr=[];
  for(let i = 0;i<(arr.length);i+size){
    let subArr=[]
    subArr.push(arr.splice(i,size))
    console.log(`subArr = ${subArr}`)
    newArr.push(subArr)
  }
  console.log(`newArr = ${newArr}`)
  return newArr;
}

chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4); 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Chunky Monkey

Link to the challenge:

Have you tried removing the console.logs from inside the function and console.logging the function itself?

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4))

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.