-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-app.ts
More file actions
56 lines (46 loc) · 1.28 KB
/
chat-app.ts
File metadata and controls
56 lines (46 loc) · 1.28 KB
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
import readline from "readline-sync"
class chatNode {
public data : string;
public reply1 : chatNode | null;
public reply2 : chatNode | null;
constructor(data: string){
this.data = data;
this.reply1 = null;
this.reply2 = null;
}
}
class Thread {
public main : chatNode | null;
constructor(main?:chatNode){
this.main = main || null;
}
public insert(node: chatNode | null = this.main, value: string): chatNode{
if(node === null) return new chatNode(value)
else{
if(node.reply1 === null){
node.reply1 = this.insert(node.reply1, value)
}else {
node.reply2 = this.insert(node.reply2, value)
}
return node
}
}
reply (){
const reply = readline.question("Write something: ")
this.insert(this.main, reply)
return this.main
}
seeChatHistory(){
console.log(this.main)
}
}
//test
const testMsg = new chatNode("Hello Kalibutan!")
const threadSample = new Thread(testMsg)
threadSample.reply()
threadSample.reply()
threadSample.reply()
threadSample.reply()
threadSample.reply()
threadSample.reply()
threadSample.seeChatHistory()