Chunky Monkey's mess

/jshint funcscope:true/
function chunkArrayInGroups(arr, size) {
var numarrs = arr.length++ / size;
for (var e = 0; e < arr.length–; e += numarrs) {
for (var i = 0; i < numarrs; i++) {
var answer = arr.push(arr[i++]);
}
}
return answer;
}

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

Well, if you try to apply this function it will return 5… I know there are some mathematical imprecisions but thats not the reason im concerned about… Why the hell does this function return a number? Its for real, i cannot figure it out xD

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/chunky-monkey

arr.push(...) will return the length of the array after you’ve pushed the element. For example, if an array has 5 elements, and you push banana to the end, then the returned value will be numeric, in this case, 6, but, the value of arr will now be an array that has banana on the end.

Here’s a quick example.

let example = ["apple", "orange", "pear"];
let resultOfPushing = example.push("banana");

console.log(example); // ["apple", "orange", "pear", "banana"]
console.log(resultOfPushing); // 4 (same as below)
console.log(example.length); // 4 (same as above)

Let’s take a look at your code

var answer = arr.push(arr[i++]);

Here, you’re setting answer to the return value of arr.push(...). As previously discussed, this means answer is now the length of the array, after performing the push operation.

Then, you return this variable

return answer;

And so the return value of the function is numeric.

ProTip: If you surround your code with three backticks, then it’ll be formatted a little better in the forums.

I didnt know the .push method could behave that way… i use to use it for adding elements to an array hahaha

It’ll do that too! It just tried to give you something useful back. I’m not sure what you were trying to set answer to, though. Perhaps the actual new array?

(See my edit to the previous post)

Yes, but it doesnt work. And i saw your edit when you did it hahahaha.

I’m not sure what you mean by “doesn’t work”. You set answer to the result of .push(), and it did what you told it to. What did you actually want to set answer to? :slightly_smiling_face:

Yes it did what i told it to do but i was expecting another result xD

You’ll need to explain what result you wanted if you want us to help you.

[Unless your issue is solved now and you wish to correct the code yourself? :slightly_smiling_face:]

I corrected the code by using other methods, but im still concerned about it because i dont understand my mistake and i dont want it to happen again. I expected that the result would be an array with the length of numarrs.

Hmm, I’d love to take a look at your updated code!

I suppose what you were expecting was that .push() returned the new value of the array. .concat() does that, but it doesn’t change the array–so it’s not much use either. You may have to split the code over two lines, like so…

arr.push(arr[i++])
answer = arr

(It all depends on what your new code looks like, though, so please do send it along!)

Im now doing the Mutations challange in another tab while I post this, so its kinda difficult to find my answer right now but late i will post it.

but basicly i can tell you that i used the most effective method of all times… yeah, copy-paste method xDDD so it would be useless to post it. I dont use to do it, actually its the first time i did it but i ask for help to a friend who works on this and he almost solve it for me so then i didnt want to type the code myself anymore after i see his solution… i dont know if you know that feeling hahahaha