28 |
29 | Sequence of operations will be shown here !
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/11. Text Editor (Stack)(Undo functionality)/index.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/11. Text Editor (Stack)(Undo functionality)/readme.md:
--------------------------------------------------------------------------------
1 |
2 | https://user-images.githubusercontent.com/56497318/127524563-8c7b6ead-0211-4c95-835c-1ca082f59834.mp4
3 |
4 |
5 | ## Text Editor
6 | ## Description
7 | Write, Delete and Undo text operations.
8 |
9 | ## Skills nurtured:
10 | Used Stack for undo functionality.
11 |
12 | ## Tech. Stack:
13 | HTML, CSS, JS
14 |
--------------------------------------------------------------------------------
/11. Text Editor (Stack)(Undo functionality)/script.js:
--------------------------------------------------------------------------------
1 |
2 | import { Stack } from './stack.js';
3 |
4 | document.onkeydown = function(event) {
5 | if (event.ctrlKey || event.metaKey) {
6 | event.preventDefault();
7 | }
8 | };
9 |
10 | onload = function () {
11 | // Get reference to elements
12 | const textbox = document.getElementById('comment');
13 | const undo = document.getElementById('undo');
14 | const clear = document.getElementById('clear');
15 | const temptext = document.getElementById('temptext');
16 |
17 | textbox.value = "";
18 | let text = "";
19 | let stack = new Stack();
20 |
21 | textbox.onclick = function () {
22 | textbox.selectionStart = textbox.selectionEnd = textbox.value.length;
23 | };
24 |
25 | clear.onclick = function () {
26 | stack.clear();
27 | text = "";
28 | textbox.value = "";
29 | temptext.innerHTML = "Sequence of operations will be shown here !";
30 | };
31 |
32 | textbox.oninput = function(event){
33 | // console.log(event);
34 | switch(event.inputType){
35 | case "insertText":
36 | stack.push(0, event.data);
37 | break;
38 | case "deleteContentBackward":
39 | stack.push(1, text[text.length-1]);
40 | break;
41 | }
42 |
43 | temptext.innerHTML = "On stack "+stack.top()+" "+temptext.innerHTML;
44 | text = textbox.value;
45 | };
46 |
47 | undo.onclick = function () {
48 | let operation = stack.pop();
49 | if(operation[0]!==-1){
50 | temptext.innerHTML = "Performing undo operation "+temptext.innerHTML;
51 | if(operation[0] === 0){
52 | let len = operation[1].length;
53 | textbox.value = textbox.value.substring(0,textbox.value.length-len);
54 | } else{
55 | textbox.value += operation[1];
56 | }
57 | text = textbox.value;
58 | }
59 | };
60 | };
--------------------------------------------------------------------------------
/11. Text Editor (Stack)(Undo functionality)/stack.js:
--------------------------------------------------------------------------------
1 | export { Stack }
2 |
3 | class Stack{
4 | constructor(){
5 | this.size = 0;
6 | this.buffer = 4;
7 | this.stack = [];
8 | }
9 |
10 | clear(){
11 | this.size = 0;
12 | this.stack = [];
13 | }
14 |
15 | isEmpty(){
16 | return ( this.size === 0 );
17 | }
18 |
19 | top(){
20 | return this.stack[this.size-1];
21 | }
22 |
23 | pop(){
24 | if(!this.isEmpty()) {
25 | this.size--;
26 | return this.stack.pop();
27 | } else{
28 | return [-1,''];
29 | }
30 | }
31 |
32 | push(type, char){
33 | if(this.isEmpty()){
34 | if(type===0)
35 | this.stack.push([type, char]);
36 | } else{
37 | let tmp = this.top();
38 | if(tmp[0]===type && tmp[1].length < this.buffer){
39 | let top = this.pop();
40 | top[1] = char + top[1];
41 | this.stack.push(top);
42 | } else{
43 | this.stack.push([type, char]);
44 | }
45 | }
46 | this.size++;
47 | }
48 | }
--------------------------------------------------------------------------------
/11. Text Editor (Stack)(Undo functionality)/style.css:
--------------------------------------------------------------------------------
1 | html,
2 | body {
3 | height: 100%;
4 | }
5 |
6 | .text_box {
7 | width: 50%;
8 | height: 100%;
9 | border: 1px solid lightgray;
10 | display: flex;
11 | flex-wrap: wrap;
12 | align-content: center;
13 | }
14 |
15 | #container{
16 | width: 100%;
17 | height: 70%;
18 | background-color: white;
19 | display: flex;
20 | margin: 0 auto;
21 | }
22 |
23 | #undo{
24 | margin:auto;
25 | display:block;
26 | }
27 |
28 |
29 | #clear{
30 | margin:auto;
31 | display:block;
32 | }
--------------------------------------------------------------------------------
/12. File Zipper(Huffman Encoding)/Notes/12.Huffman Encoding File Zipper.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fsiddh/Data-Structures-in-Real-Life-Projects/837705d13cc46efc18f5fce61a4585fda7d0eda3/12. File Zipper(Huffman Encoding)/Notes/12.Huffman Encoding File Zipper.pdf
--------------------------------------------------------------------------------
/12. File Zipper(Huffman Encoding)/README.md:
--------------------------------------------------------------------------------
1 | # CBHuffman
2 | CB Huffman
3 |
--------------------------------------------------------------------------------
/12. File Zipper(Huffman Encoding)/heap.js:
--------------------------------------------------------------------------------
1 | export { BinaryHeap }//will be received by huffman.js
2 |
3 | class BinaryHeap {
4 |
5 | constructor() {
6 | this.heap = []; //array
7 | }
8 |
9 | insert(value) {//function to insert values in heap array
10 | this.heap.push(value);//pushed value in heap array
11 | this.bubbleUp();//called function heapify to manage heap(max node as parent)
12 | }
13 |
14 | size() {
15 | return this.heap.length;
16 | }
17 |
18 | empty(){
19 | return ( this.size()===0 );
20 | }
21 |
22 | //using iterative approach
23 | bubbleUp() {//heapify
24 | let index = this.size() - 1;
25 |
26 | while (index > 0) {
27 | let element = this.heap[index],
28 | parentIndex = Math.floor((index - 1) / 2),
29 | parent = this.heap[parentIndex];
30 |
31 | if (parent[0] >= element[0]) break;
32 | this.heap[index] = parent;
33 | this.heap[parentIndex] = element;
34 | index = parentIndex
35 | }
36 | }
37 |
38 | extractMax() {
39 | const max = this.heap[0];//root node
40 | const tmp = this.heap.pop();
41 | if(!this.empty()) {
42 | this.heap[0] = tmp;
43 | this.sinkDown(0);
44 | }
45 | return max;
46 | }
47 |
48 | sinkDown(index) {
49 |
50 | let left = 2 * index + 1,
51 | right = 2 * index + 2,
52 | largest = index;
53 | const length = this.size();
54 |
55 | // console.log(this.heap[left], left, length, this.heap[right], right, length, this.heap[largest]);
56 |
57 | if (left < length && this.heap[left][0] > this.heap[largest][0]) {
58 | largest = left
59 | }
60 | if (right < length && this.heap[right][0] > this.heap[largest][0]) {
61 | largest = right
62 | }
63 | // swap
64 | if (largest !== index) {
65 | let tmp = this.heap[largest];
66 | this.heap[largest] = this.heap[index];
67 | this.heap[index] = tmp;
68 | this.sinkDown(largest)
69 | }
70 | }
71 | }
--------------------------------------------------------------------------------
/12. File Zipper(Huffman Encoding)/huffman.js:
--------------------------------------------------------------------------------
1 | import { BinaryHeap } from './heap.js';
2 |
3 | export { HuffmanCoder } //will be recieved by script.js
4 |
5 | class HuffmanCoder{
6 |
7 | stringify(node){//para -> root node
8 | if(typeof(node[1])==="string"){
9 | return '\''+node[1]; //leaf node ->adding additional '\' to detect char in string form
10 | }
11 |
12 | return '0' + this.stringify(node[1][0]) + '1' + this.stringify(node[1][1]); // inorder traversal
13 | }
14 |
15 | display(node, modify, index=1){
16 | if(modify){
17 | node = ['',node];
18 | if(node[1].length===1)
19 | node[1] = node[1][0];
20 | }
21 |
22 | if(typeof(node[1])==="string"){
23 | return String(index) + " = " + node[1];
24 | }
25 |
26 | let left = this.display(node[1][0], modify, index*2);
27 | let right = this.display(node[1][1], modify, index*2+1);
28 | let res = String(index*2)+" <= "+index+" => "+String(index*2+1);
29 | return res + '\n' + left + '\n' + right;
30 | }
31 |
32 | destringify(data){//creating tree again from string tree structure
33 | let node = [];
34 | if(data[this.ind]==='\''){
35 | this.ind++;
36 | node.push(data[this.ind]);
37 | this.ind++;
38 | return node;
39 | }
40 |
41 | this.ind++;
42 | let left = this.destringify(data);
43 | node.push(left);
44 | this.ind++;
45 | let right = this.destringify(data);
46 | node.push(right);
47 |
48 | return node;
49 | }
50 |
51 | getMappings(node, path){//para->root node,empty string
52 | //node[0] -> freq
53 | //leaf node[1] = char else node[1] = array of children
54 | //node[1][0] -> left child
55 | //node[1][1] -> right child
56 |
57 | if(typeof(node[1])==="string"){//leaf node detection(will be be a char(a|b|c|d etc) js identifies char as type string)
58 | this.mappings[node[1]] = path; //saves path at index of ascii value of requested char in array MAPPINGS
59 | return;
60 | }
61 |
62 | //DFS
63 | this.getMappings(node[1][0], path+"0");//left child path+0
64 | this.getMappings(node[1][1], path+"1");//right child path+1
65 | }
66 |
67 | encode(data){//function to encode //will return values to script.js
68 | //'data'is from script.js(uploaded text file is 'data')
69 | this.heap = new BinaryHeap();//generate heap
70 |
71 | //STEP 1 -> CREATE FREQUENCY MAP
72 | const mp = new Map();//generate map named mp
73 | for(let i=0;i MAKE MIN HEAP
82 | for(const key in mp){
83 | this.heap.insert([-mp[key], key]);//putting negative values to convert max heap to min heap
84 | } //value 10 2 if values are neg -10,-2
85 | //max heap-> 10 ,2 //min heap->-2,-10
86 |
87 | //STEP 3 -> TILL ONE NODE REMAINS
88 | // DO -> 1.EXTRACT TOP TWO MIN NODES
89 | // 2.CREATE A NODE WITH LEFT AND RIGHT CHILD AS EXTRACTED NODE
90 | //ITS FREQ = SUM OF FREQ OF BOTH EXTRACTED NODES
91 | // 3.INSERT NEW NODE IN HEAP
92 | while(this.heap.size() > 1){//till one node remains
93 | const node1 = this.heap.extractMax(); //extraction top two values
94 | const node2 = this.heap.extractMax();
95 | //freq //char children
96 | const node = [node1[0]+node2[0],[node1,node2]];
97 | this.heap.insert(node); //insert new node
98 | }
99 | const huffman_encoder = this.heap.extractMax(); //extract the last remaining root node
100 |
101 | this.mappings = {};//contains encoded value for each char
102 | this.getMappings(huffman_encoder, "");//function call
103 |
104 | let binary_string = "";
105 | for(let i=0;i needed additional bits to make binary string multiple of 8
110 | let rem = (8 - binary_string.length%8)%8; //we need to convert binary string to ascii codes to save in text -> every 8 bit code = 1 ascii char ->hence we need bits to be multiple of 8 so as to convert all bits to ascii char
111 | //**LATER WE NEED TO REMOVE THSI ADDITIONAL BITS WHILE DECODING**
112 | let padding = "";
113 | for(let i=0;ia),comp ratio
132 | return [final_res, this.display(huffman_encoder, false), info];
133 | //values returned to script.js
134 | }
135 |
136 | decode(data){
137 | data = data.split('\n');//getting 3 elements of final_res(tree in string,additional bits,encoded text)
138 | if(data.length===4){
139 | // if there was a newline char in text
140 | data[0] = data[0] + '\n' + data[1];
141 | data[1] = data[2];
142 | data[2] = data[3];
143 | data.pop();
144 | }
145 |
146 | this.ind = 0;
147 | const huffman_decoder = this.destringify(data[0]);//get tree back
148 | const text = data[2];//encoded text
149 |
150 | let binary_string = "";
151 |
152 | //converting encoded text(via ascii code) back to binary code
153 | for(let i=0;i GO LEFT IF 0 | GO RIGHT FOR 1 -> LEAF NODE = CHAR
171 | for(let i=0;i
2 |
3 |
4 |
5 |
6 |
7 | File Zipper
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
22 |
23 |
24 |
25 | Tree Structure Will Be Displayed Here !!
26 |
27 |
28 |
29 |
30 | Operation info will be shown here !!
31 |
32 |
33 |
34 |
35 |
36 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/12. File Zipper(Huffman Encoding)/index.php:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/12. File Zipper(Huffman Encoding)/script.js:
--------------------------------------------------------------------------------
1 | import { HuffmanCoder } from './huffman.js';
2 |
3 |
4 | onload = function () {
5 | // Get reference to elements
6 | const treearea = document.getElementById('treearea');//space for tree
7 | const encode = document.getElementById('encode');//encode button
8 | const decode = document.getElementById('decode');//decode button
9 | const temptext = document.getElementById('temptext');//space for text
10 | const upload = document.getElementById('uploadedFile');//uploaded file
11 |
12 | const coder = new HuffmanCoder();
13 |
14 | upload.addEventListener('change',()=>{ alert("File uploaded") });//when file is uploaded will display an alert
15 |
16 | encode.onclick = function () {//on clicking encode button this function is triggered
17 |
18 | const uploadedFile = upload.files[0];//because we are uploading 1 file we need that first file uploaded by upload.files function
19 | if(uploadedFile===undefined){//if nothing is there in uploadedFile ->result = undefined
20 | alert("No file uploaded !");
21 | return;
22 | }
23 | const fileReader = new FileReader();
24 | fileReader.onload = function(fileLoadedEvent){//will see text file now
25 | const text = fileLoadedEvent.target.result;//will get all the written text in file
26 | if(text.length===0){//file is blank
27 | alert("Text can not be empty ! Upload another file !");
28 | return;
29 | }
30 | let [encoded, tree_structure, info] = coder.encode(text);//trigger encode function in huffman.js and will recieve the returned values from function(encoded text,tree structure,compression ratio)
31 | downloadFile(uploadedFile.name.split('.')[0] +'_encoded.txt', encoded);//triggers function downloadFile
32 | treearea.innerText = tree_structure;//in left space for tree -> display a tree and ..
33 | treearea.style.marginTop = '2000px'; //..
34 | temptext.innerText = info; //compression ratio
35 | };
36 | fileReader.readAsText(uploadedFile, "UTF-8");
37 | };
38 |
39 | decode.onclick = function () {//on clicking decode button this function triggers
40 |
41 | const uploadedFile = upload.files[0];//because we are uploading 1 file we need that first file uploaded by upload.files function
42 | if(uploadedFile===undefined){//if nothing is there in uploadedFile ->result = undefined
43 | alert("No file uploaded !");
44 | return;
45 | }
46 | const fileReader = new FileReader();
47 | fileReader.onload = function(fileLoadedEvent){//will see text file now
48 | const text = fileLoadedEvent.target.result;//will get all the written text in file
49 | if(text.length===0){//file is blank
50 | alert("Text can not be empty ! Upload another file !");
51 | return;
52 | }
53 | let [decoded, tree_structure, info] = coder.decode(text);//trigger decode function in huffman.js and will recieve the returned values from function(decoded text,tree structure,compression ratio)
54 | downloadFile(uploadedFile.name.split('.')[0] +'_decoded.txt', decoded);//triggers function downloadFile
55 | treearea.innerText = tree_structure;//in left space for tree -> display a tree and ..
56 | treearea.style.marginTop = '2000px';
57 | temptext.innerText = info; //compression ratio
58 | };
59 | fileReader.readAsText(uploadedFile, "UTF-8");
60 | };
61 |
62 | };
63 |
64 | function downloadFile(fileName, data){//file name ->firstletter+_encoded.txt //data is encoded text recieved from encode function
65 | let a = document.createElement('a');
66 | a.href = "data:application/octet-stream,"+encodeURIComponent(data);
67 | a.download = fileName;
68 | a.click();
69 | }
--------------------------------------------------------------------------------
/12. File Zipper(Huffman Encoding)/style.css:
--------------------------------------------------------------------------------
1 | html,
2 | body {
3 | height: 100%;
4 | }
5 |
6 | p{
7 | font-family:'Brush Script MT';
8 | font-size :15mm;
9 | font-weight: bold;
10 | }
11 |
12 | .text_box {
13 | padding: 30px;
14 | width: 50%;
15 | height: 100%;
16 | border: 1px solid lightgray;
17 | display: flex;
18 | flex-wrap: wrap;
19 | align-content: center;
20 | }
21 |
22 | #container{
23 | width: 100%;
24 | height: 70%;
25 | background-color: white;
26 | display: flex;
27 | margin: 0 auto;
28 | }
29 |
30 | .center_buttons{
31 | margin:auto;
32 | display:block;
33 | }
--------------------------------------------------------------------------------
/12. File Zipper(Huffman Encoding)/zip.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fsiddh/Data-Structures-in-Real-Life-Projects/837705d13cc46efc18f5fce61a4585fda7d0eda3/12. File Zipper(Huffman Encoding)/zip.png
--------------------------------------------------------------------------------
/12. File Zipper(Huffman Encoding)/zip2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fsiddh/Data-Structures-in-Real-Life-Projects/837705d13cc46efc18f5fce61a4585fda7d0eda3/12. File Zipper(Huffman Encoding)/zip2.png
--------------------------------------------------------------------------------
/13. Chatbot(APIs)/README.md:
--------------------------------------------------------------------------------
1 | # ChatBot
2 | ChatBot
3 |
--------------------------------------------------------------------------------
/13. Chatbot(APIs)/bot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fsiddh/Data-Structures-in-Real-Life-Projects/837705d13cc46efc18f5fce61a4585fda7d0eda3/13. Chatbot(APIs)/bot.png
--------------------------------------------------------------------------------
/13. Chatbot(APIs)/chat_tree.json:
--------------------------------------------------------------------------------
1 | {
2 | "child_msg" : ["Want to hear a joke ?","What about some news ?","Wanna get motivated ?","Terminate chat"],
3 | "children" : [
4 | {
5 | "child_msg" : ["Funny Joke","Very funny Joke","PJ :)","Terminate chat"],
6 | "children" : [
7 | {
8 | "message" : "getJoke()",
9 | "type" : "function"
10 | },
11 | {
12 | "message" : "Santa Banta Ha Ha Ha",
13 | "type" : "normal"
14 | },
15 | {
16 | "message" : "Banta Santa",
17 | "type" : "normal"
18 | }
19 | ]
20 | },
21 | {
22 | "child_msg" : ["Local News","National News","World News","Terminate chat"],
23 | "children" : [
24 | {
25 | "message" : "getNews()",
26 | "type" : "function"
27 | },
28 | {
29 | "message" : "India is the best",
30 | "type" : "normal"
31 | },
32 | {
33 | "message" : "India becomes a super power",
34 | "type" : "normal"
35 | }
36 | ]
37 | },
38 | {
39 | "message" : "You're amazing and everything you do will make a positive effect on the world"
40 | }
41 | ]
42 | }
--------------------------------------------------------------------------------
/13. Chatbot(APIs)/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Document
8 |
10 |
11 |
12 |
13 |
14 |
18 |
19 |
20 |
21 |
22 |
23 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
Chat with CB Bot
39 |
Code your way to Success
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
74 |
75 |
86 |
87 |
88 |
89 |
90 | Hey there friends !
91 | This is a simple yet amazing chatbot tutorial
92 | You'll learn about JSON, Javascript promises and APIs
93 | After this tutorial you can even feed your own data to the bot and have fun with it !
94 | So let's begin interacting with the CB Bot.
95 | Send a simple hello message to begin
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
--------------------------------------------------------------------------------
/13. Chatbot(APIs)/index.php:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/13. Chatbot(APIs)/script.js:
--------------------------------------------------------------------------------
1 | onload = function(){
2 | // outputs a javascript object from the parsed json
3 |
4 | var chat = {
5 | messageToSend: '',
6 | messageResponses: [
7 | 'Why did the web developer leave the restaurant? Because of the table layout.',
8 | 'How do you comfort a JavaScript bug? You console it.',
9 | 'An SQL query enters a bar, approaches two tables and asks: "May I join you?"',
10 | 'What is the most used language in programming? Profanity.',
11 | 'What is the object-oriented way to become wealthy? Inheritance.',
12 | 'An SEO expert walks into a bar, bars, pub, tavern, public house, Irish pub, drinks, beer, alcohol'
13 | ],
14 | init: async function() {
15 | this.chatTree = new ChatTree();
16 | await this.chatTree.init();
17 | this.cacheDOM();
18 | this.bindEvents();
19 | await this.render();
20 | },
21 | cacheDOM: function() {
22 | this.$chatHistory = $('.chat-history');
23 | this.$button = $('button');
24 | this.$textarea = $('#message-to-send');
25 | this.$chatHistoryList = this.$chatHistory.find('ul');
26 | },
27 | bindEvents: function() {
28 | this.$button.on('click', this.addMessage.bind(this));
29 | this.$textarea.on('keyup', this.addMessageEnter.bind(this));
30 | },
31 | render: async function() {
32 | this.scrollToBottom();
33 | if (this.messageToSend.trim() !== '') {
34 | var template = Handlebars.compile( $("#message-template").html());
35 | var context = {
36 | messageOutput: this.messageToSend,
37 | time: this.getCurrentTime()
38 | };
39 |
40 | this.input = this.messageToSend;
41 | this.$chatHistoryList.append(template(context));
42 | this.scrollToBottom();
43 | this.$textarea.val('');
44 |
45 | // responses
46 | var templateResponse = Handlebars.compile( $("#message-response-template").html());
47 | var contextResponse = {
48 | response: await this.chatTree.getMessage(this.input),
49 | time: this.getCurrentTime()
50 | };
51 |
52 | setTimeout(function() {
53 | this.$chatHistoryList.append(templateResponse(contextResponse));
54 | this.scrollToBottom();
55 | }.bind(this), 1000);
56 |
57 | }
58 |
59 | },
60 |
61 | addMessage: function() {
62 | this.messageToSend = this.$textarea.val();
63 | this.render();
64 | },
65 | addMessageEnter: function(event) {
66 | // enter was pressed
67 | if (event.keyCode === 13) {
68 | this.addMessage();
69 | }
70 | },
71 | scrollToBottom: function() {
72 | this.$chatHistory.scrollTop(this.$chatHistory[0].scrollHeight);
73 | },
74 | getCurrentTime: function() {
75 | return new Date().toLocaleTimeString().
76 | replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3");
77 | }
78 | };
79 |
80 | chat.init();
81 | };
82 |
83 | class ChatTree {
84 |
85 | constructor() {
86 | }
87 |
88 | async init(){
89 | const data = await this.reset();
90 | this.chat_tree = data;
91 | this.firstMsg = true;
92 | console.log("inside done");
93 | return "Chat has now been terminated. Send hi to begin chat again !";
94 | }
95 |
96 | async reset(){
97 | const response = await fetch('chat_tree.json');
98 | const jsonResponse = await response.json();
99 | return jsonResponse;
100 | }
101 |
102 | async getMessage(input){
103 | let resp = '';
104 | //input = new String(input.trim());
105 | //console.log(input);
106 | if(this.firstMsg===true) {
107 | this.firstMsg = false;
108 | resp += "Hey there buddy ";
109 | } else {
110 |
111 | if(("message" in this.chat_tree) && (input.trim()==="Reset")) {
112 | return this.init();
113 | }
114 |
115 | if(isNaN(parseInt(input)) || parseInt(input)<=0 || parseInt(input) > this.chat_tree['children'].length+1)
116 | return 'It seems like you gave a wrong input ! Go ahead try again !';
117 |
118 | if(parseInt(input)-1===this.chat_tree['children'].length){
119 | this.init();
120 | }
121 |
122 | this.chat_tree = this.chat_tree['children'][parseInt(input)-1];
123 | }
124 |
125 | if("message" in this.chat_tree){
126 | let data;
127 | if(this.chat_tree['type']==="function"){
128 | // console.log(String(this.chat_tree['message']),String("getJoke()"));
129 | if(this.chat_tree['message']==="getJoke()"){
130 | data = await eval(this.chat_tree['message']);
131 | data = data.value.joke;
132 | } else{
133 | data = await eval(this.chat_tree['message']);
134 | data = data.articles[0].title;
135 | }
136 | } else{
137 | data = this.chat_tree['message'];
138 | }
139 | resp += data;
140 | resp += "
40 |
49 | You'll receive a 2D array as input. Each column stores
50 | rewards on higher and lower platform. You have to return an
51 | array stating 1 if you want to jump to higher platform, 0
52 | otherwise. However, after selecting 1 you have to select 0
53 | the next time. Can you solve it ?
54 |
55 |