function numberOfRoutes(start, end, currentDistance = 0, maxDistance = 0) {
  let routes = 0;

  if (testHashMap.hasOwnProperty(start)) {
    const nextNodes = Object.keys(testHashMap[start]);
    nextNodes.forEach((keyOfNextNode) => {
      const newDistance = currentDistance + testHashMap[start][keyOfNextNode];
      if (newDistance >= maxDistance) { //end of the recursion
        return 0;
      }

      if (keyOfNextNode === end) {
        routes++;
      }
      //even though touched the target of the point, can still continue
      routes += numberOfRoutes(keyOfNextNode, end, newDistance, maxDistance);
    });
  } else {
    throw new Error("NO SUCH ROUTE");
  }

  return routes;
}
/**
 * To calculate the number of routes with a max distance.
 * @param start
 * @param end
 * @param currentDistance
 * @param maxDistance
 * @returns {number}
 */
To calculate the number of routes with a max distance.
To calculate the number of routes with a max distance .
function numberOfRoutes(start, end, currentDistance = 0, maxDistance = 0) {
  let routes = 0;
  if (testHashMap.hasOwnProperty(start)) {
    const nextNodes = Object.keys(testHashMap[start]);
    nextNodes.forEach((keyOfNextNode) => {
      const newDistance = currentDistance + testHashMap[start][keyOfNextNode];
      if (newDistance >= maxDistance) { 
        return 0;
      }
      if (keyOfNextNode === end) {
        routes++;
      }
      routes += numberOfRoutes(keyOfNextNode, end, newDistance, maxDistance);
    });
  } else {
    throw new Error("NO SUCH ROUTE");
  }
  return routes;
}
function numberOfRoutes ( start , end , currentDistance = 0 , maxDistance = 0 ) { let routes = 0 ; if ( testHashMap . hasOwnProperty ( start ) ) { const nextNodes = Object . keys ( testHashMap [ start ] ) ; nextNodes . forEach ( ( keyOfNextNode ) => { const newDistance = currentDistance + testHashMap [ start ] [ keyOfNextNode ] ; if ( newDistance >= maxDistance ) { return 0 ; } if ( keyOfNextNode === end ) { routes ++ ; } routes += numberOfRoutes ( keyOfNextNode , end , newDistance , maxDistance ) ; } ) ; } else { throw new Error ( "NO SUCH ROUTE" ) ; } return routes ; }
To calculate the number of routes with a max distance.
To calculate the number of routes with a max distance .
//end of the recursion //even though touched the target of the point, can still continue