Your team wants to know how many clicks a user needs to get from one page of a web app to another. Write breadthFirstSearch(links, from, to), which returns the shortest path of pages from from to to (both included), or null if to is unreachable.
links[page] lists the pages that page links to. Links are one-way. A page with no key has no outgoing links.
const links = {
home: ['products', 'help'],
products: ['product-42', 'cart'],
'product-42': ['cart'],
cart: ['checkout'],
help: ['contact'],
};
breadthFirstSearch(links, 'home', 'checkout');
// ['home', 'products', 'cart', 'checkout'] (3 clicks)
breadthFirstSearch(links, 'home', 'home'); // ['home']
breadthFirstSearch(links, 'help', 'checkout'); // null
Spotted a mistake or a rights concern? Report an issue