---
id: 699c8e045ee7cb94ed2322d8
title: "Challenge 217: Captured Chess Pieces"
challengeType: 28
dashedName: challenge-217
---

# --description--

Given an array of strings representing chess pieces you still have on the board, calculate the value of the pieces your opponent has captured.

In chess, you start with 16 pieces:

| Piece  | Abbreviation   | Quantity | Value |
| ------ | -------------- | -------- | ----- |
| Pawn   | `"P"`          | 8        | 1     |
| Rook   | `"R"`          | 2        | 5     |
| Knight | `"N"`          | 2        | 3     |
| Bishop | `"B"`          | 2        | 3     |
| Queen  | `"Q"`          | 1        | 9     |
| King   | `"K"`          | 1        | 0     |

- The given array will only contain the abbreviations above.
- Any of the 16 pieces not included in the given array have been captured.
- Return the total value of all captured pieces, unless...
- If the King has been captured, return `"Checkmate"`.

# --hints--

`getCapturedValue(["P", "P", "P", "P", "P", "P", "R", "R", "N", "B", "Q", "K"])` should return `8`.

```js
assert.equal(getCapturedValue(["P", "P", "P", "P", "P", "P", "R", "R", "N", "B", "Q", "K"]), 8);
```

`getCapturedValue(["P", "P", "P", "P", "P", "R", "B", "K"])` should return `26`.

```js
assert.equal(getCapturedValue(["P", "P", "P", "P", "P", "R", "B", "K"]), 26);
```

`getCapturedValue(["K", "P", "P", "N", "P", "P", "R", "P", "B", "P", "N", "B"])` should return `16`.

```js
assert.equal(getCapturedValue(["K", "P", "P", "N", "P", "P", "R", "P", "B", "P", "N", "B"]), 16);
```

`getCapturedValue(["P", "Q", "N", "P", "P", "B", "K", "P", "R", "R", "P", "P", "B", "P"])` should return `4`.

```js
assert.equal(getCapturedValue(["P", "Q", "N", "P", "P", "B", "K", "P", "R", "R", "P", "P", "B", "P"]), 4);
```

`getCapturedValue(["P", "K"])` should return `38`.

```js
assert.equal(getCapturedValue(["P", "K"]), 38);
```

`getCapturedValue(["N", "P", "P", "B", "K", "P", "Q", "N", "P", "P", "R", "R", "P", "P", "P", "B"])` should return `0`.

```js
assert.equal(getCapturedValue(["N", "P", "P", "B", "K", "P", "Q", "N", "P", "P", "R", "R", "P", "P", "P", "B"]), 0);
```

`getCapturedValue(["N", "P", "P", "B", "P", "R", "Q", "P", "P", "P", "B"])` should return `"Checkmate"`.

```js
assert.equal(getCapturedValue(["N", "P", "P", "B", "P", "R", "Q", "P", "P", "P", "B"]), "Checkmate");
```

# --seed--

## --seed-contents--

```js
function getCapturedValue(pieces) {

  return pieces;
}
```

# --solutions--

```js
function getCapturedValue(pieces) {
  if (!pieces.includes("K")) return "Checkmate";

  const values = { P:1, R:5, N:3, B:3, Q:9, K:0 };
  let remaining = "PPPPPPPPRRNNBBQK";

  for (const p of pieces) {
    remaining = remaining.replace(p, "");
  }

  return [...remaining].reduce((sum, p) => sum + values[p], 0);
}
```
