const roads = ["Alice's House-Bob's House", "Alice's House-Cabin", "Alice's House-Post Office"];
function buildGraph(edges) {
let graph = Object.create(null);
function addEdge(from, to) {
if (graph[from] == null) {
graph[from] = [];
}
graph[from].push(to);
}
for (let [from, to] of edges.map(r => r.split("-"))) {
addEdge(from, to);
addEdge(to, from);
}
return graph;
}
const roadGraph = buildGraph(roads);
console.log(roadGraph);
//roadGraph object:
[Object: null prototype] {
"Alice's House": [ "Bob's House", 'Cabin', 'Post Office' ],
"Bob's House": [ "Alice's House" ],
Cabin: [ "Alice's House" ],
'Post Office': [ "Alice's House" ]
}
Why do we have such inconsistencies?
- Cabin and Post Office values in Alice's House are in single quotes
- the Cabin key has no quotes
- Post Office key has single quotes