Chunky Monkey - Why is my code returning string?

Chunky Monkey - Why is my code returning string?
0.0 0

#1

Tell us what’s happening:
Why is my code returning a string?

Your code so far

function chunkArrayInGroups(arr, size) {
// Break it up.
var str = arr.join(’’);
var newArr = [];
var num= arr.length/size;

var j = 0;
for (var i = 0; i < num; i++){
newArr[i]= str.slice(j,size).split(’’);
j = size;
size +=size;

}
return newArr;

}

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

function chunkArrayInGroups(arr, size) {
  // Break it up.
  var str = arr.join('');
  var newArr = [];
  var num= arr.length/size;
  
  var j = 0;
  for (var i = 0; i < num; i++){
    newArr[i]= str.slice(j,size).split('');
    j = size;
    size  +=size;
  
}
  return newArr;
  
  
}

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

Your browser information:

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

Link to the challenge:


#2

When you split a string, the elements of the resulting array will be strings also.

Suggestion: Do not join arr into a string to begin with and just use the slice function on arr (ditching the whole .split(’’) part). This will not completely solve the challenge, but will get you a lot closer than you are now.

Question: Why are you using the following line?

var num= arr.length/size;

#3

Thank you for the explanatuon
I used var num= arr.length/size; so that if there is 6 element in the given array then i can seperate them into equal halves depending on the size provided. For example if the given array has 6 elements, and the size is 2 then length/size=3. My intension was to divide those 6 elements into 3 different sub array with 2 element on each, but i guess it doesnt work. One stupid question, how do experience developer think about how to solve algorithms,does it just come by experience or do i have to keep thinking hard to solve the algorithms.