---
id: 69f8c998d78ad3171a0713bc
title: "Challenge 289: Sum of Differences"
challengeType: 28
dashedName: challenge-289
---

# --description--

Given an array of numbers, return the sum of the differences between each number and the one that follows it.

For example, given `[1, 3, 4]`, return `3` (2 + 1).

# --hints--

`sumOfDifferences([1, 3, 4])` should return `3`.

```js
assert.equal(sumOfDifferences([1, 3, 4]), 3);
```

`sumOfDifferences([5, -3, 3, 9, 10])` should return `5`.

```js
assert.equal(sumOfDifferences([5, -3, 3, 9, 10]), 5);
```

`sumOfDifferences([9, 6, 15, -20, 33, 14, 25, 16, -7])` should return `-16`.

```js
assert.equal(sumOfDifferences([9, 6, 15, -20, 33, 14, 25, 16, -7]), -16);
```

`sumOfDifferences([50, 102, -46, 82, -49, 29, 71, 902, -237, 111, -61, 75])` should return `25`.

```js
assert.equal(sumOfDifferences([50, 102, -46, 82, -49, 29, 71, 902, -237, 111, -61, 75]), 25);
```

# --seed--

## --seed-contents--

```js
function sumOfDifferences(arr) {

  return arr;
}
```

# --solutions--

```js
function sumOfDifferences(arr) {
  return arr.slice(0, -1).reduce((sum, n, i) => sum + (arr[i + 1] - n), 0);
}
```
