0

Trust you doing great !!!1

Trying to create a directed graph between airports using AdjacencyList shown in YT Video using Map function in Node.js and ran the below code in online editor pramp.com,problem is why Map is showing the output as Empty...It should show up a Map like this ,also if you can explain what this line of code does routes.forEach(route => addEdge(...route)) why 3 dots and fat arrow =>

enter image description here

Question is is this problem with online editor Pramp.com ? I dont want to use VSCode hence using online editor

            //The Data

            const airports = 'PHX BKK OKC JFK LAX MEX EZE HEL LOS LAP LIM'.split(' ');

            const routes = [

            ['PHX','LAX'],
            ['PHX','JFK'],
            ['JFK','OKC'],
            ['JFK','HEL'],
            ['MEX','LAX'],
            ['MEX','BKK'],
            ['MEX','LIM'],
            ['MEX','EZE'],
            ['LIM','BKK'],

            ];

            //The Graph

            const adjacencyList = new Map();

            //add-node
            function addnode(airport){

            adjacencyList.set(airport,[]);

            //Add edge,undirected
            function addEdge(origin,destination){
            adjacencyList.get(origin).push(destination);
            adjacencyList.get(destination).push(origin);

            }

            //Create the Graph
            airports.forEach(addNode);
            routes.forEach(route => addEdge(...route))



            }

            console.log(adjacencyList);

In Pramp editor i see this

enter image description here

Many Thanks In advance

Carolyn

1 Answer 1

1

Ellipsis (...) is the spread operator. It takes an array, for example [origin, destination] and basically turns it into two vars origin, destination. Calling

addEdge(...[origin,destination])

is like calling

addEdge([origin,destination][0], [origin,destination][1])

Which is the same as

addEdge(origin,destination)

The "fat arrow" operator is a lambda (anonymous function) definition.

(a,b) => { console.log(a, b) }

is the same as

function (a,b) { console.log(a, b) }

Its just a shorter way to define functions without names. Usually simple ons that are given as parameters. In this case to the forEach call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.