Skip to curriculum
Jump to the editor

3.5. Array.prototype.concat

Easy
Topics
10–20 min

concat joins arrays into a new one: the first page of results and the next, the default options and the user's. It looks simple, but it has a precise rule for what gets spread: real arrays are spread one level, everything else is added as a single item, and a special symbol can change the answer for any object.

Write Array.prototype.myConcat(...items) in concat.js. It must behave like the built-in Array.prototype.concat for every case below.

Signature

JAVASCRIPT
Array.prototype.myConcat = function myConcat(...items) // returns a new array
  • this and then each of items, in order, go into the result.
  • A value that is spreadable adds its items; any other value is added as one item.
  • Returns a new array. Nothing that was passed in is changed.

Requirements

  1. myConcat returns a new array, even when it has no arguments, and changes neither this nor the arguments.
  2. An array argument is spread one level: its items are added, but an array inside it is added as that same array.
  3. A value that is not an array (a number, a string, null, undefined, an object) is added as one item, as it is.
  4. Holes are kept: a hole in this or in a spread argument is a hole at the matching index of the result, and holes at the end still count in the result's length.
  5. An array-like object and a string are not spread. An object whose Symbol.isConcatSpreadable property is set is spread when that value is truthy and not spread when it is falsy, whether it is an array or not.
  6. this follows the same rule as the arguments: a string this becomes a String object added as one item; an array-like this is added as one item.
  7. Each spread value's length is read once and converted to a whole number from 0 up: "2" is 2, 2.9 is 2, -3 is 0. A length that is a BigInt or a Symbol, or an object whose valueOf returns one, throws a TypeError, as the built-in does.
  8. It throws a TypeError when called on null or undefined.

Examples

JAVASCRIPT
[1, 2].myConcat([3, 4], [5]);       // [1, 2, 3, 4, 5]
[0].myConcat(1, "ab", null);        // [0, 1, "ab", null]
[0].myConcat([[1]], [2, [3]]);      // [0, [1], 2, [3]]: one level only
JAVASCRIPT
[1, , 3].myConcat([4, , 6]);        // [1, empty, 3, 4, empty, 6]: length 6
[1].myConcat({ 0: "a", length: 1 }); // [1, { 0: "a", length: 1 }]: not spread
JAVASCRIPT
const marked = { 0: "a", 1: "b", length: 2, [Symbol.isConcatSpreadable]: true };
["x"].myConcat(marked);              // ["x", "a", "b"]

Environment

  • Plain JavaScript in a browser page; the tests call Array.prototype.myConcat from concat.js.
  • concat.js starts with "use strict". Keep it: in strict mode, this inside myConcat is exactly the value it was called on, so a call on null can be detected.
  • Not allowed: the built-in concat, slice, flat and flatMap. The tests delete them from Array.prototype while myConcat runs.
  • Out of scope: subclasses of Array (myConcat always returns a plain array), and results longer than 2^53 - 1 items.

Hints

  1. The result's index and a source's index are different numbers. What moves the result's index forward when a source has a hole?
  2. If the last source ends with holes, nothing is ever written there. How does the result still get the right length?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…