Add Elements to the End of an Array Using concat Instead of push... Wrong test answer?

Tell us what’s happening:
I got the answer correct, but I don’t think this is actually the right way to emulate the push method in a non-muting way. If I’m correct, the result should be: [1, 2, 3, [4, 5]]. However, the answer that passes the tests is: [1, 2, 3, 4, 5]. Can someone please confirm whether I’m right or if I’ve overlooked something? I’m more concerned about understanding the lessons than I am with getting the answers right.


Your code so far

function nonMutatingPush(original, newItem) {
  // Add your code below this line
  return original.concat(newItem);
  // Add your code above this line
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingPush(first, second);

Your browser information:

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

Link to the challenge:

Array.concat merge the content of two arrays.

So you have

// array of integers
var first = [1, 2, 3];
var second = [4, 5];

first.concat(second) [1,2,3,4,5]
// you add THE CONTENT of second into first.
// in this case the content is 4 and 5.

The case [1, 2, 3, [4, 5]] would be if second is an Array of Array.

var first = [1, 2, 3]; // array of int
var second = [ [4, 5] ]; // array of array

in this case the content of second is an array.

Hope it helps.

I think that you are correct in feeling that something is not quite right with that challenge. The fact that the results were not the same for both the existing function and the one you were to code did confuse things.

I believe you have the right answer though. And clearly you understood the content well enough to spot the flaw so I’ll throw in another :white_check_mark: for curiosity

1 Like

The purpose of this challenge is emulate the Array.prototype.push() method and do not mutate the array. So, the correct answer must be [1, 2, 3, 4, 5]. The elements of second array must to be added each iteration.