---
id: 69c918841987bf16c1d1bacb
title: Step 20
challengeType: 1
dashedName: step-20
---

# --description--

Once the main loop finishes, you need to decide which node(s) to display results for.

If a specific `targetNode` was provided, only show results for that node. Otherwise, show results for all nodes.

In JavaScript, you can get all indices of an array as an iterable using `[...Array(n).keys()]`.

After the outer `for` loop, create a variable named `targets`. Using a ternary expression, if `targetNode` is strictly not `null`, assign `[targetNode]`, otherwise assign `[...Array(n).keys()]`.

# --hints--

You should create a variable `targets` after the main loop.

```js
assert.match(
  __helpers.removeJSComments(shortestPath.toString()),
  /(const|let|var)\s+targets\s*=/
);
```

Your `targets` variable should use a ternary checking `targetNode !== null`.

```js
assert.match(
  __helpers.removeJSComments(shortestPath.toString()),
  /targets\s*=\s*targetNode\s*!==\s*null\s*\?/
);
```

When a target is provided, `targets` should be `[targetNode]`, otherwise it should be `[...Array(n).keys()]`.

```js
assert.match(
  __helpers.removeJSComments(shortestPath.toString()),
  /\?\s*\[\s*targetNode\s*\]\s*:\s*\[\s*\.\.\.\s*Array\s*\(\s*n\s*\)\s*\.keys\s*\(\s*\)\s*\]/
);
```

# --seed--

## --seed-contents--

```js
const INF = Infinity;
const adjMatrix = [
  [0, 5, 3, INF, 11, INF],
  [5, 0, 1, INF, INF, 2],
  [3, 1, 0, 1, 5, INF],
  [INF, INF, 1, 0, 9, 3],
  [11, INF, 5, 9, 0, INF],
  [INF, 2, INF, 3, INF, 0],
];

function shortestPath(matrix, startNode, targetNode = null) {
  const n = matrix.length;
  const distances = new Array(n).fill(INF);
  distances[startNode] = 0;
  const paths = Array.from({ length: n }, (_, i) => [i]);
  const visited = new Array(n).fill(false);
  
  for (let i = 0; i < n; i++) {
    let minDistance = INF;
    let current = -1;
    for (let nodeNo = 0; nodeNo < n; nodeNo++) {
      if (!visited[nodeNo] && distances[nodeNo] < minDistance) {
        minDistance = distances[nodeNo];
        current = nodeNo;
      }
    }
    if (current === -1) {
      break;
    }
    visited[current] = true;
    
    for (let nodeNo = 0; nodeNo < n; nodeNo++) {
      const distance = matrix[current][nodeNo];
      if (distance !== INF && !visited[nodeNo]) {
        const newDistance = distances[current] + distance;
        if (newDistance < distances[nodeNo]) {
          distances[nodeNo] = newDistance;
          paths[nodeNo] = [...paths[current], nodeNo];
        }
      }
    }
  }
  
--fcc-editable-region--
  
--fcc-editable-region--
}
```
