# ES2015 Lesson 1: Arrow Functions – Solutions

In this lesson, both the video and the text versions are available to you. If you have any feedback, contact me at info@zsoltnagy.eu.

Exercise 1: Write an arrow function that returns the string `'Hello World!'`.

Solution:

Exercise 2: Write an arrow function that expects an array of integers, and returns the sum of the elements of the array. Use the built-in method reduce on the array argument.

Solution:

Reduce works with an accumulator to store the value associated with reducing the array, and takes two arguments:

• the initial value of the accumulator,
• a function to define the operation between the accumulator and the upcoming element of the array.

Reduce performs the following operations:

• `( 0, 1 ) => 0 + 1` becomes `1`,
• `( 1, 2 ) => 1 + 2` becomes `3`,
• `( 3, 3 ) => 3 + 3` becomes `6`,
• `( 6, 4 ) => 6 + 4` becomes `10`,
• `( 10, 5 ) => 10 + 5` becomes `15`.

Exercise 3: Rewrite the following code by using arrow functions wherever it makes sense to use them:

Solution:

Notes:

• it does not make sense to replace the `Entity` constructor, because we need the context
• it does not make sense to replace the prototype extension `greet`, as we make use of its default context
• it makes perfect sense to replace the function argument of `setTimeout` with an arrow function. Notice that the context binding also disappeared in the solution