---
id: 69b1028d6e265413d0198a2d
title: "Challenge 234: Prank Number"
challengeType: 28
dashedName: challenge-234
---

# --description--

Given an array of numbers where all but one number follow a pattern, return a new array with the one number that doesn't follow the pattern fixed.

The pattern will be one of:

- The numbers increase from one to the next by a fixed amount (addition).
- The numbers decrease from one to the next by a fixed amount (subtraction).

For example, given `[2, 4, 7, 8, 10]` return `[2, 4, 6, 8, 10]`.

# --hints--

`fixPrankNumber([2, 4, 7, 8, 10])` should return `[2, 4, 6, 8, 10]`.

```js
assert.deepEqual(fixPrankNumber([2, 4, 7, 8, 10]), [2, 4, 6, 8, 10]);
```

`fixPrankNumber([10, 10, 8, 7, 6])` should return `[10, 9, 8, 7, 6]`.

```js
assert.deepEqual(fixPrankNumber([10, 10, 8, 7, 6]), [10, 9, 8, 7, 6]);
```

`fixPrankNumber([12, 24, 36, 48, 61, 72, 84, 96])` should return `[12, 24, 36, 48, 60, 72, 84, 96]`.

```js
assert.deepEqual(fixPrankNumber([12, 24, 36, 48, 61, 72, 84, 96]), [12, 24, 36, 48, 60, 72, 84, 96]);
```

`fixPrankNumber([4, 1, -2, -5, -8, -5])` should return `[4, 1, -2, -5, -8, -11]`.

```js
assert.deepEqual(fixPrankNumber([4, 1, -2, -5, -8, -5]), [4, 1, -2, -5, -8, -11]);
```

`fixPrankNumber([0, 100, 200, 300, 150, 500])` should return `[0, 100, 200, 300, 400, 500]`.

```js
assert.deepEqual(fixPrankNumber([0, 100, 200, 300, 150, 500]), [0, 100, 200, 300, 400, 500]);
```

`fixPrankNumber([400, 425, 400, 375, 350, 325, 300])` should return `[450, 425, 400, 375, 350, 325, 300]`.

```js
assert.deepEqual(fixPrankNumber([400, 425, 400, 375, 350, 325, 300]), [450, 425, 400, 375, 350, 325, 300]);
```

`fixPrankNumber([-5, 5, 10, 15, 20])` should return `[0, 5, 10, 15, 20]`.

```js
assert.deepEqual(fixPrankNumber([-5, 5, 10, 15, 20]), [0, 5, 10, 15, 20]);
```

# --seed--

## --seed-contents--

```js
function fixPrankNumber(arr) {

  return arr;
}
```

# --solutions--

```js
function fixPrankNumber(arr) {
  const diffs = arr.slice(1).map((v, i) => v - arr[i]);

  let step = diffs[0];
  for (let i = 0; i < diffs.length - 1; i++) {
    if (diffs[i] === diffs[i + 1]) {
      step = diffs[i];
      break;
    }
  }

  const result = [...arr];

  for (let i = 0; i < result.length - 1; i++) {
    if (result[i + 1] - result[i] !== step) {
      if (i === 0 && result[2] - result[1] === step) {
        result[0] = result[1] - step;
      } else {
        result[i + 1] = result[i] + step;
      }
      break;
    }
  }

  return result;
}
```
