Skip to curriculum
Jump to the editor

3.8. Array.prototype.square and average on every array

Easy
Topics
10–20 min

Every array already has map, filter and join, though none of them is stored on the array itself: they live on one shared object that every array inherits from. Adding your own methods there makes them available on every array in the page, old and new. This is how polyfills work, and it is also how a small helper can quietly turn into a problem for other code.

Write two methods in array-math.js so that every array has them: square() and average().

Signature

JAVASCRIPT
[1, 2, 3].square()  // returns a new array: [1, 4, 9]
[1, 2, 3].average() // returns a number: 2
  • square(): a new array with each item multiplied by itself. Holes stay holes.
  • average(): the sum of the items divided by how many items there are. Each item is converted with Number(). An empty array gives NaN.

Requirements

  1. Every array has both methods, however it was made (a literal, new Array, Array.of, Array.from, or a subclass of Array).
  2. The methods are stored once and shared: they are not own properties of each array, and Object.keys of an array still lists only its indexes.
  3. Each method works on the array it is called on, and neither changes that array.
  4. square() returns a new array of the squares, keeps holes, converts number strings ("3" gives 9), and returns [] for an empty array.
  5. average() returns the mean of the items converted with Number(). Number strings count as numbers (["1", "2"] gives 1.5, not a mean of text). An item that Number() cannot read, such as "a" or undefined, makes the result NaN. Number()'s other rules apply as they are: null and "" count as 0, true as 1 and false as 0, so [null].average() is 0.
  6. Holes are not items: [1, , 3].average() is 2. An empty array gives NaN.
  7. The methods chain: square() returns an array, so .average() can follow.

Examples

JAVASCRIPT
[1, 2, 3, -4].square();          // [1, 4, 9, 16]
[1, , 3].square();               // [1, empty, 9]
JAVASCRIPT
[1, 2, 3, 4].average();          // 2.5
["1", "2"].average();            // 1.5
[1, , 3].average();              // 2: two items
[].average();                    // NaN
JAVASCRIPT
[1, 2, 3, 4].square().average(); // 7.5

Environment

  • Plain JavaScript in a browser page; array-math.js runs first, then the tests call the methods on arrays they create.
  • The built-in array methods (map, forEach, reduce…) are allowed.
  • Making the methods non-enumerable, so that for…in does not list them, is problem 3.9.

Hints

  1. An arrow function has no this of its own. Which kind of function gets the array before the dot as its this?
  2. For an array with holes, is this.length the number of items?

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…