Skip to main content

Chaining exercises

Method chaining is a pattern in JavaScript where multiple functions are called on the same object consecutively. For example:

TeamMembers
.filter(teamMember => teamMember.age > 10)
.sort((a, b) => a.age - b.age)

In the following exercises you're going to practice a few array methods. If you don't have a CodePen account create a new one. You can fork the exercises to your own account so your progress will be saved!

Pro tip: You can hide the HTML & CSS tabs by dragging the JS section over the other two. This gives you plenty of space to work with.

More tips

  • In these exercises, you will never have to use a return statement!
  • Think about the number of items you want to end up with, this can help you decide how to start
  • Having selected the right window, using Cmd ⌘ Shift F formats your code
  • Check MDN for a list of methods
  • You can use the spread operator ... to overwrite a key in an object: {...teamMember, age: 27}
  • Use the right array method. For example: .some returns a boolean, while .find returns the first value that satisfies the given condition.