call(back)
FrontendmediumSign in to star problems

Implement Array.prototype.concat

Write Array.prototype.myConcat(...items) so it matches the built-in concat. The grader disables the native map, filter, reduce and concat while your code runs.

Rules

  • Start from this, then each item in order. Spread arrays one level, plus objects whose Symbol.isConcatSpreadable is true, keeping their holes.
  • Every other value is appended as it is — strings and plain objects included.
  • Always return a new array, even with no arguments. Define the method with function, not an arrow.
[1].myConcat([2, [3]], 4)      // [1, 2, [3], 4]
[1].myConcat("ab", { a: 1 })  // [1, "ab", { a: 1 }]

Tests write a hole as "<hole>"; the grader turns it into a real empty slot and back. An input written { spreadable: [...] } becomes an array-like object with Symbol.isConcatSpreadable set.

Hints

Hint 1

Walk [this, ...items]. Spread a value when it is an object whose Symbol.isConcatSpreadable is true, or, when that is undefined, when it is an array.

Hint 2

Write by index with your own counter, skipping holes, and set out.length at the end so trailing holes survive.

Asked at