-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (92 loc) · 2.95 KB
/
index.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
91
92
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const FizzBuzz = require("./FizzBuzz");
processRequests();
async function processRequests() {
while (true) {
await processRequest();
}
}
async function getValue() {
var stdin = process.openStdin();
console.log('type value to evaluate');
var getValue = new Promise((resolve, reject) => {
stdin.addListener("data", function (d) {
stdin.removeAllListeners();
resolve(Number(d.toString().trim()));
});
});
return getValue;
}
async function continueAddingRules(rules) {
var stdin = process.openStdin();
console.log('Current rules: ');
console.log(rules.map(rule => rule.description));
console.log('Do you want to add any rules(y/n)?');
var getValue2 = new Promise((resolve, reject) => {
stdin.addListener("data", function (d) {
stdin.removeAllListeners();
resolve(d.toString().trim());
});
});
let addingRulesAnswer = await getValue2;
return addingRulesAnswer == "y";
}
async function addRule(rules) {
var stdin = process.openStdin();
console.log("Type keyword for one of available rules: Fizz, Fezz, Bang, Bong, Buzz, Empty, Reverse, Part2");
var getValue3 = new Promise((resolve, reject) => {
stdin.addListener("data", function (d) {
stdin.removeAllListeners();
resolve(d.toString().trim());
});
});
let ruleType = await getValue3;
switch (ruleType) {
case "Fizz":
rules.push(FizzBuzz.FizzRule);
break;
case "Fezz":
rules.push(FizzBuzz.FezzRule);
break;
case "Bang":
rules.push(FizzBuzz.BangRule);
break;
case "Bong":
rules.push(FizzBuzz.BongRule);
break;
case "Buzz":
rules.push(FizzBuzz.BuzzRule);
break;
case "Bang":
rules.push(FizzBuzz.BangRule);
break;
case "Empty":
rules.push(FizzBuzz.EmptyRule);
break;
case "Reverse":
rules.push(FizzBuzz.ReverseRule);
break;
case "Part2":
rules.push(FizzBuzz.Part2Rule);
break;
default:
}
return rules;
}
async function processRequest() {
let value = await getValue();
let rules = [];
let addingRules = true;
while (addingRules) {
addingRules = await continueAddingRules(rules);
if (addingRules) {
rules = await addRule(rules);
}
}
let finalrule = rules.reduce((prevRule, currentRule, i, a) => prevRule.followedBy(currentRule), new FizzBuzz.Rule((i) => false, (s, i) => s));
let finalWords = finalrule.applyRule([], value);
if (finalWords.length != 0) {
console.log(finalWords.reduce((p, c, i, a) => p + c), '');
}
}