-
Notifications
You must be signed in to change notification settings - Fork 1
/
bondz.ts
61 lines (49 loc) · 1.51 KB
/
bondz.ts
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
type ListOfPoints = {
name: string;
connections: Array<string>
}
function pathBetweenPoints(pointsArr: Array<ListOfPoints>, x: string, y: string): string {
let result: string[] = [];
let path: string = `${x}`
result.push(x)
let points: string[] = pointsArr.map(point => point.name)
console.log(points)
if (!(points.includes(x)) || !(points.includes(y))) {
return 'no path';
}
function helperNext(str: string, y: string): void {
if (str === y) {
return;
}
let newStr: ListOfPoints = pointsArr.find(point => point.name === str)
let nextStr: string = newStr.connections[0]
let nextOneStr: string = newStr.connections[1]
let newNext = '';
if (result.includes(nextStr)) {
result.push(nextOneStr)
newNext = nextOneStr;
} else {
result.push(nextStr)
newNext = nextStr
}
return helperNext(newNext, y)
}
helperNext(x, y)
if (result.length <= 1) {
return 'no path'
} else {
for (let i = 1; i < result.length; i++) {
path += ` -> ${result[i]} `
}
}
return path;
}
let listOfPoints = [
{ name: "A", connections: ["B", "C"] },
{ name: "B", connections: ["A", "E"] },
{ name: "C", connections: ["A", "D"] },
{ name: "D", connections: ["C"] },
{ name: "E", connections: ["B", "F"] },
{ name: "F", connections: ["E"] },
]
console.log(pathBetweenPoints(listOfPoints, "A", "B"))