---
id: 69c918841987bf16c1d1bacc
title: Step 21
challengeType: 1
dashedName: step-21
---

# --description--

Now loop through the target nodes to display results for each one.

Add a `for...of` loop that iterates over `targets` using `nodeNo` as the loop variable. Leave the body empty for now.

# --hints--

You should create a `for...of` loop with the variable `nodeNo` iterating over `targets`.

```js
assert.match(
  __helpers.removeJSComments(code),
  /for\s*\(\s*(const|let)\s+nodeNo\s+of\s+targets\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];
        }
      }
    }
  }
  
  const targets = targetNode !== null ? [targetNode] : [...Array(n).keys()];
--fcc-editable-region--
  
--fcc-editable-region--
}
```
