---
id: 69c918841987bf16c1d1bac5
title: Step 14
challengeType: 1
dashedName: step-14
---

# --description--

After scanning all nodes to find the closest unvisited one, you need to handle the case where no valid node is found. This happens when all remaining nodes are unreachable.

After the inner `for` loop, but still inside the outer loop, add an `if` statement that checks if `current` is strictly equal to `-1`. If that is true, use `break` inside the `if` block to stop the outer loop early.

# --hints--

You should add an `if` statement that checks if `current === -1`.

```js
assert.match(
  __helpers.removeJSComments(shortestPath.toString()),
  /if\s*\(\s*current\s*===\s*-1\s*\)/
);
```

You should use `break` inside the `if` block.

```js
assert.match(
  __helpers.removeJSComments(shortestPath.toString()),
  /if\s*\(\s*current\s*===\s*-1\s*\)[\s\S]*?break/
);
```

# --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;
      }
    }
--fcc-editable-region--
    
--fcc-editable-region--
  }
}
```
