Here’s one of the very basic Exercises I do with most of my students. This is actually one of my favourite ones, since it teaches them many things:

  • the basics of functional programming
  • synchronous callbacks
  • this keyword
  • prototype

And all these concepts in just seven lines of code counting the curly brackets 🙂

Array.prototype.forEach = cb => {
  for (let i = 0; i < this.length; i++) {
    if (typeof cb === 'function') {
      cb(this[i], i);
    }
  }
}