Skip to content

Instantly share code, notes, and snippets.

@mizanmahi
Created December 21, 2021 16:55
Show Gist options
  • Save mizanmahi/d6f4d2b618b4d3363194c5a327fc49f9 to your computer and use it in GitHub Desktop.
Save mizanmahi/d6f4d2b618b4d3363194c5a327fc49f9 to your computer and use it in GitHub Desktop.
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