-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocialGraph.js
90 lines (83 loc) · 2.82 KB
/
socialGraph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(() => {
class Node {
constructor() {
this.connections = [];
}
}
class Graph {
constructor() {
// Declare network object and length
this.network = {};
this.length = 0;
}
add(x) {
// Create a new node
const node = new Node();
// Only if the node (x) is not already in the network
// add it and increase the length
if (!this.network[x]) {
this.network[x] = node;
this.length++;
return true;
}
return;
}
remove(x) {
// If x is not in the network, remove it
if (!this.network[x]) return `${x} is not in the network`;
this.removeConnections(x);
delete this.network[x];
}
findConnection(x, y) {
// If x is in the network...
if (!this.network[x]) `${x} is not in the network`
// If x has no connections at all
if (!this.network[i].connections.length) {
return `${x} has no connections. You can connect if you'd like.`;
}
// If y is in x's connections, return true
for (let i = 0; i < xConnections.length; i ++) {
if (xConnections[i] === y) return `${y} is connected to ${x}!`;
return `${x} has no connection to ${y}. Add a connection. You can be buds.`;
}
}
addConnection(x, y) {
// If x or y is not in the network, nope
if (!this.network[x]) return `${x} is not in the network`;
if (!this.network[y]) return `${y} is not in the network`;
// If x exists but has no connections, connect x and y
if (!this.network[x].connections.length) {
this.network[x].connections.push(y);
this.network[y].connections.push(x);
return `${x} and ${y} are now connected`;
}
// Iterate through x's connections and connect them
// if they're not already connected
for (let i = 0; i < this.network[x].connections.length; i ++) {
if (this.network[x].connections[i] !== y) {
this.network[x].connections.push(y);
this.network[y].connections.push(x);
return `${y} is now connected to ${x}`;
}
}
}
removeConnection(x, y) {
// Get out if x doesn't exist
if (!this.network[x]) return `${x} is not in the network`;
// Otherwise, iterate through x's connections and remove y
// If there is no length the function will return undefined
for (let i = 0; i < this.network[x].connections.length; i++) {
if (this.network[x].connections[i] === y) {
this.network[x].connections.splice(i, 1);
}
}
for (let i = 0; i < this.network[y].connections.length; i++) {
if (this.network[y].connections[i] === x) {
this.network[y].connections.splice(i, 1);
}
}
return `${x} and ${y} are no longer connected`;
}
}
const graph = new Graph();
})();