Problem

Have you ever wondered how you’d create your own implementation of Array in JavaScript? No? Oh, well give it a shot anyway.

Implement array so that the code below works.

const friends = array("Ben", "Lynn", "Alex")

friends.push("Mikenzi") // 4
friends.pop() // Mikenzi
friends.filter((friend) => {
  return friend.length === 4
})

This may seem strange (it is), but it makes for a good learning activity which I know is the first thing you want to do in the morning when you wake up and open this newsletter.

Solution

This isn’t terribly practical, but it is a fun activity to do to really grasp the often overlooked bits of JavaScript.

function array () {
  let arr = Object.create(array.prototype)

  Object.defineProperty(arr, 'length', {
    value: 0,
    enumerable: false,
    writable: true,
  })

  for (key in arguments) {
    arr[key] = arguments[key]
    arr.length += 1
  }

  return arr
}

array.prototype.push = function (element) {
  this[this.length] = element
  this.length++
  return this.length
}

array.prototype.pop = function () {
  this.length--
  const elementToRemove = this[this.length]
  delete this[this.length]
  return elementToRemove
}

array.prototype.filter = function (cb) {
  let result = array()

  for (let index in this) {
    if (this.hasOwnProperty(index)) {
      const element = this[index]

      if (cb(element, index)) {
        result.push(element)
      }
    }
  }

  return result
}

const friends = array("Ben", "Lynn", "Alex")

friends.push("Mikenzi") // 4
friends.pop() // Mikenzi
friends.filter((friend) => {
  return friend.length === 4
})

If you’re curious or confused by the code, we wrote about it here.