Chunky Monkey's mess

Chunky Monkey's mess
0.0 0

#1

/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:


#2

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.


#3

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


#4

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)


#5

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


#6

Check out the push documentation and it tells you exactly what it does and what it returns. You can look up all the JavaScript functions and methods on MDN webdocs and see all the information concerning what a function does, what it returns, and how to use it.


#7

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:


#8

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


#9

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:]


#10

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.


#11

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!)


#12

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.


#13

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