diff --git a/maths/factorial.js b/maths/factorial.js index 647a3e225..c13878d90 100644 --- a/maths/factorial.js +++ b/maths/factorial.js @@ -48,6 +48,6 @@ function calcFactorial (num) { } // Run `factorial` Function to find average of a list of numbers. -/* global prompt */ -var num = prompt('Enter a number: ') + +var num = console.log('Enter a number: ') console.log(calcFactorial(num)) diff --git a/maths/find_lcm.js b/maths/find_lcm.js index 3d4d0e8a6..60470e37e 100644 --- a/maths/find_lcm.js +++ b/maths/find_lcm.js @@ -3,7 +3,7 @@ license: GPL-3.0 or later Modified from: - https://github.com/TheAlgorithms/Python/blob/master/maths/find_lcm.py + https://github.com/TheAlgorithms/Python/blob/master/maths/findLcm.py More about LCM: https://en.wikipedia.org/wiki/Least_common_multiple diff --git a/maths/graph.js b/maths/graph.js index f97fc3cf0..3d9873759 100644 --- a/maths/graph.js +++ b/maths/graph.js @@ -1,95 +1,88 @@ -// create a graph class -class Graph { - // defining vertex array and - // adjacent list - constructor(noOfVertices) - { - this.noOfVertices = noOfVertices; - this.AdjList = new Map(); - } - - // functions to be implemented - - // addVertex(v) - // addEdge(v, w) - // printGraph() - - // bfs(v) - // dfs(v) +// create a graph class +class Graph { + // defining vertex array and + // adjacent list + constructor (noOfVertices) { + this.noOfVertices = noOfVertices + this.AdjList = new Map() + } + + // functions to be implemented + + // addVertex(v) + // addEdge(v, w) + // printGraph() + + // bfs(v) + // dfs(v) + + // add vertex to the graph + addVertex (v) { + // initialize the adjacent list with a + // null array + + this.AdjList.set(v, []) + } + + // add edge to the graph + addEdge (v, w) { + // get the list for vertex v and put the + // vertex w denoting edge between v and w + this.AdjList.get(v).push(w) + + // Since graph is undirected, + // add an edge from w to v also + this.AdjList.get(w).push(v) + } + + // Prints the vertex and adjacency list + printGraph () { + // get all the vertices + const getKeys = this.AdjList.keys() + + // iterate over the vertices + for (const i of getKeys) { + // great the corresponding adjacency list + // for the vertex + const getValues = this.AdjList.get(i) + let conc = '' + + // iterate over the adjacency list + // concatenate the values into a string + for (const j of getValues) { + conc += j + ' ' + } + + // print the vertex and its adjacency list + console.log(i + ' -> ' + conc) + } + } } - -// add vertex to the graph -addVertex(v) -{ - // initialize the adjacent list with a - // null array - this.AdjList.set(v, []); -} - -// add edge to the graph -addEdge(v, w) -{ - // get the list for vertex v and put the - // vertex w denoting edge between v and w - this.AdjList.get(v).push(w); - - // Since graph is undirected, - // add an edge from w to v also - this.AdjList.get(w).push(v); -} - - -// Prints the vertex and adjacency list -printGraph() -{ - // get all the vertices - var get_keys = this.AdjList.keys(); - - // iterate over the vertices - for (var i of get_keys) -{ - // great the corresponding adjacency list - // for the vertex - var get_values = this.AdjList.get(i); - var conc = ""; - - // iterate over the adjacency list - // concatenate the values into a string - for (var j of get_values) - conc += j + " "; - - // print the vertex and its adjacency list - console.log(i + " -> " + conc); - } -} - - // Example -var graph = new Graph(6); -var vertices = [ 'A', 'B', 'C', 'D', 'E', 'F' ]; - -// adding vertices -for (var i = 0; i < vertices.length; i++) { - g.addVertex(vertices[i]); -} - -// adding edges -g.addEdge('A', 'B'); -g.addEdge('A', 'D'); -g.addEdge('A', 'E'); -g.addEdge('B', 'C'); -g.addEdge('D', 'E'); -g.addEdge('E', 'F'); -g.addEdge('E', 'C'); -g.addEdge('C', 'F'); - -// prints all vertex and -// its adjacency list -// A -> B D E -// B -> A C -// C -> B E F -// D -> A E -// E -> A D F C -// F -> E C -g.printGraph(); +const graph = new Graph(6) +const vertices = ['A', 'B', 'C', 'D', 'E', 'F'] +// adding vertices +for (let i = 0; i < vertices.length; i++) { + graph.addVertex(vertices[i]) +} + +// adding edges +graph.addEdge('A', 'B') +graph.addEdge('A', 'D') +graph.addEdge('A', 'E') +graph.addEdge('B', 'C') +graph.addEdge('D', 'E') +graph.addEdge('E', 'F') +graph.addEdge('E', 'C') +graph.addEdge('C', 'F') + +// prints all vertex and +// its adjacency list +// A -> B D E +// B -> A C +// C -> B E F +// D -> A E +// E -> A D F C +// F -> E C +graph.printGraph()