Tell us what’s happening:
Your code so far
function chunkArrayInGroups(arr, size) {
let newArray=[]
let numitems=arr.length/size
let pointer=0
for(let i=0;i<numitems;i++)
{
let item=arr.slice(pointer,pointer+size)
newArray.push(item)
pointer+=size
}
if(arr.length%size!==0){
let item=arr.slice(pointer,pointer+arr.length%size)
console.log(item)
newArray.push(item)
newArray.pop()
}console.log(newArray)
return newArray
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3)
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; rv:86.0) Gecko/20100101 Firefox/86.0
.
Challenge: Chunky Monkey
Link to the challenge:
miku86
March 24, 2021, 9:29am
2
Hey there,
nice to meet you!
Can you tell us with a simple example, e.g. (["a", "b", "c", "d"], 2)
,
what your code does step by step?
change to
if(arr.length%size!==0){
let item=arr.slice(pointer,pointer+arr.length%size)
// console.log(item)
newArray.push(item)
i get an empty array item as last item in newArray which i have to pop
for call
([0, 1, 2, 3, 4, 5, 6], 3) i get [[0,1,2],[3,4,5],[6],[ ]]
num items is number of small arrays of size 2 as given array size for [“a”, “b”, “c”, “d”] is 4
num items 2 which is number of times for loop runs to splice with pointer to start at first element if array is not perfecly divisible it takes remainder as array size and pointer which is at last item of largest perfect divisor in if statement i get a empty array pushed in newArray i dont know why
@kalpakwadettiwar Not really sure what you are asking. Your code works fine and passes the challenge, however the following code is not even needed.
if(arr.length%size!==0){
let item=arr.slice(pointer,pointer+arr.length%size)
console.log(item)
newArray.push(item)
newArray.pop()
}console.log(newArray)
system
Closed
September 23, 2021, 4:28am
7
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.