Tell us what’s happening:
Your code so far
function chunkArrayInGroups(arr, size) {
// Break it up.
var myarr = [];
for(var i=0;i<arr.length;i+=size){
myarr.push(arr.slice(i,size));
}
return myarr;
}
chunkArrayInGroups(["a", "b", "c", "d","e","f"], 2);
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/66.4.102 Chrome/60.4.3112.102 Safari/537.36
.
Link to the challenge:
slice(i,size)
will extract elements from index i to size(exclusive) unlike in splice where size
number of elements are extracted from the index i
.so basically the statement inside the for loop is doing this:
i=0 slice(0,2) myarr={["a","b"]}
i=1 slice(2,2) myarr={["a","b"],[]} //this is because you are trying to slice elemnt s
from index 2 to 2
i=1 slice(4,2) myarr={["a","b"],[],[]}//again trying to slice elemnt from index 4 to index 2
what you could do is change myarr.push(arr.slice(i,size+i));
so that it extracts elements from i to i+size
i=0 slice(0,2) myarr={["a","b"]}
i=2 slice(2,4) myarr={["a", "b"], ["c", "d"]}
i=4 slice(4,6) myarr={["a", "b"], ["c", "d"],["e","f"]}
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice visit this link for info about slice
this would work as expected