Created
December 21, 2021 16:55
-
-
Save mizanmahi/d6f4d2b618b4d3363194c5a327fc49f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const array = [ | |
['a', ['m', 'n', 'o']], | |
['b', ['s', 't', 'u']], | |
['d', ['e', 'f', 'g']], | |
] | |
function getShortestPath(target, array){ | |
let shortest = array[0]; | |
let shortestDistance = Infinity; | |
for(let i = 0; i < array.length; i++){ | |
let distance = array[i][1].indexOf(target); | |
if(distance !== -1){ | |
if(distance < shortestDistance){ | |
shortest = array[i]; | |
shortestDistance = distance; | |
} | |
} | |
} | |
return `${shortest[0]} => ${target}` ; | |
} | |
console.log(getShortestPath('f', array)); // d => f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment