-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathfizzsolver.js
More file actions
153 lines (128 loc) · 4.31 KB
/
fizzsolver.js
File metadata and controls
153 lines (128 loc) · 4.31 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"use strict";
const fetch = require("node-fetch");
const basePath = "https://api.noopschallenge.com";
const startPath = "/fizzbot";
// GET method with node-fetch
async function tryGet(url) {
let result = {};
try {
let response = await fetch(url);
result = await response.json();
} catch (error) {
console.log("Something Bad Happened!!")
console.log(error);
}
return result;
}
// POST method with node-fetch
async function tryPost(url, data) {
let result = {}
try {
let response = await fetch(url, {
method: "post",
body: JSON.stringify(data),
headers: {"Content-Type": "application/json"}
});
result = await response.json();
} catch (error) {
console.log("Something Bad Happened!!")
console.log(error);
}
return result;
}
// Calculate the answer with the rules and the numbers
function calcAnswer(rules, numbers) {
let answer = [];
try {
for (let n of numbers) {
let tempo = [];
for (let r of rules) {
if (n % r["number"] === 0) {
tempo.push(r["response"]);
}
}
let result = tempo.join("");
if (result === "") {
result = String(n);
}
answer.push(result);
}
} catch (error) {
console.log("Something Bad Happened!!")
console.log(error);
}
return {"answer": answer.join(" ")};
}
// Main solver function
const startSolver = async (url) => {
let cont = true;
let answer = {};
// main loop
while (cont) {
// send GET request to url
console.log("GET the question...")
let res = await tryGet(url);
if (JSON.stringify(res) === "{}") {
console.log("Oops, something is wrong!!");
cont = false;
break;
}
if (typeof res["message"] !== "undefined") {
// GET response contains message, show it
console.log("====== GET message begins ======");
console.log(res["message"]);
console.log("====== GET message ends ======");
}
if (typeof res["nextQuestion"] !== "undefined") {
// GET response contains the nextQuestion link, go there
url = basePath + res["nextQuestion"]
continue;
}
if (typeof res["rules"] !== "undefined" && typeof res["numbers"] !== "undefined") {
// it's a real question
let rules = res["rules"];
let numbers = res["numbers"];
// show the rules
console.log("====== Rules begins ======");
console.log(JSON.stringify(rules))
console.log("====== Rules ends ======");
// show the numbers
console.log("====== Numbers begins ======");
console.log(JSON.stringify(numbers))
console.log("====== Numbers ends ======");
// calculate the answer
answer = calcAnswer(rules, numbers);
} else {
// the first question or something, answer "JavaScript!!" anyway
answer = {"answer": "JavaScript!!"};
}
// show our decided answer
console.log("====== Our answer ======");
console.log(JSON.stringify(answer));
console.log("====== Answer ends ======");
// POST the answer back to the url
console.log("POST the answer...")
res = await tryPost(url, answer);
if (JSON.stringify(res) === "{}") {
console.log("Oops, something is wrong!!");
cont = false;
break;
}
if (typeof res["message"] !== "undefined") {
// POST response contains message, show it
console.log("====== POST message begins ======");
console.log(res["message"]);
console.log("====== POST message ends ======");
}
if (typeof res["nextQuestion"] !== "undefined") {
// POST response contains the nextQuestion link, go there
url = basePath + res["nextQuestion"]
continue;
}
// No next question?
console.log("No more questions, test finished!!");
cont = false;
}
}
// Entry point
startSolver(basePath + startPath);