Chunky Monkey function only works on numbers

Chunky Monkey function only works on numbers
0.0 0

#1

Tell us what’s happening:
My code works fine for numbers but doesn’t work for strings. I can’t find anything in the MDN documentation that explains this behavior.

Your code so far

function chunkArrayInGroups(arr, size) {
 var result = [];
  for (var i = 0; i < arr.length; i += size) {
    result.push(arr.slice(arr[i], arr[i] + size));
  } return result;
  
} 
chunkArrayInGroups(["a", "b", "c", "d"], 2);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:


#2

Another unexpected result is if I test using an array of numbers starting at 1 (eg. [1, 2, 3, 4, 5, 6]) it starts the first sub-array at index 1 instead of 0. What is the cause of this?


#3

slice has two parameters. The first tells slice at which index to start and the second indicates which index to slice up to but not include in the new array created from the slice. What this means for your solution is you need to be referencing an array index (a number from 0 to the array length - 1). Your current code uses the actual element value instead of the index (variable i).


#4

Thanks HEAPS. solution:

function chunkArrayInGroups(arr, size) {
var index = [];
var result = [];
for (var i = 0; i < arr.length; i += size) {
index.push(i);
result.push(arr.slice(i, i + size));
} return result;

}
chunkArrayInGroups([“a”, “b”, “c”, “d”], 2);