-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtn.js
179 lines (166 loc) · 5.93 KB
/
gtn.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* global createNumberCol createNumberRow createCheckboxRow createButtonGroupRow createRow createAlert exit keyDownHandler keyUpHandler */
const defaultLowerLimit = 100;
const defaultUpperLimit = 999;
const defaultGuessLimit = 10;
const defaultPositions = true;
const defaultBiggerSmaller = true;
const defaultSumCheck = false;
let lowerLimit;
let upperLimit;
let guessLimit;
let positions;
let biggerSmaller;
let sumCheck;
let number;
let digits;
let sum;
let guesses;
let oldGuesses;
window.locked = true;
const form = document.getElementById('form');
const rowClass = 'row justify-content-center';
const colClass = 'col col-md-3 col-lg-2 mb-3';
const numberClass = 'col-3 col-md-2 col-xl-1 mb-3';
const buttonClass = 'col-9 col-md-7 col-lg-6 col-xl-5 my-auto';
const firstRow = [['Lower Limit', 'lowerLimit', '1', '99999'], ['Upper Limit', 'upperLimit', '1', '99999'], ['Guess Limit', 'guessLimit', '1', '9999']];
const secondRow = [['<u>P</u>ositions', 'positions', 'p'], ['<u>B</u>igger Smaller', 'biggerSmaller', 'b'], ['<u>S</u>um <span id="sum"></span>', 'sumCheck', 's']];
const numberRow = ['1. Guess', 'guess', '1', '99999'];
const buttonRow = [['success', 'if(!locked)window.guess()', 'g', 'search', '<u>G</u>uess'], ['primary', 'if(!locked)random()', 'r', 'random', '<u>R</u>andom'], ['danger', 'if(!locked)giveUp()', 'u', 'times', 'Give <u>U</u>p'], ['info', 'restart()', 'e', 'sync', 'R<u>e</u>start']];
form.appendChild(createNumberRow(rowClass, colClass, firstRow));
form.appendChild(createCheckboxRow(rowClass, colClass, secondRow));
form.appendChild(createRow(rowClass, [createNumberCol(numberClass, ...numberRow), createButtonGroupRow(buttonClass, 'btn-group', buttonRow)]));
const guessLabel = form.children[2].children[0].children[0];
resetInputs();
restart();
document.getElementById('sumCheck').addEventListener('change', function () {
sumCheck = this.checked;
updateSum();
});
document.addEventListener('keydown', keyDownHandler);
document.addEventListener('keyup', keyUpHandler);
function resetInputs () {
lowerLimit = defaultLowerLimit;
upperLimit = defaultUpperLimit;
guessLimit = defaultGuessLimit;
positions = defaultPositions;
biggerSmaller = defaultBiggerSmaller;
sumCheck = defaultSumCheck;
document.getElementById('lowerLimit').value = lowerLimit;
document.getElementById('upperLimit').value = upperLimit;
document.getElementById('guessLimit').value = guessLimit;
document.getElementById('positions').checked = positions;
document.getElementById('biggerSmaller').checked = biggerSmaller;
document.getElementById('sumCheck').checked = sumCheck;
}
window.guess = function () {
const input = document.getElementById('guess');
const guess = parseInt(input.value);
input.value = '';
if (isNaN(guess)) {
createAlert('danger', 'Not a number.');
} else if (findDigitsCount(guess) !== digits) {
createAlert('danger', 'Wrong number of digits.');
} else if (checkDigits(guess)) {
createAlert('danger', 'Digists cannot repeat.');
} else if (oldGuesses.includes(guess)) {
createAlert('danger', 'Already guessed before.');
} else if (sumCheck && findDigitsSum(guess) !== sum) {
createAlert('danger', 'Wrong sum of digits.');
} else {
checkNumber(guess);
}
};
window.random = function () {
let guess;
do {
guess = Math.floor(Math.random() * (upperLimit - lowerLimit + 1) + lowerLimit);
} while (checkDigits(guess) || oldGuesses.includes(guess) || (sumCheck && findDigitsSum(guess) !== sum)); // eslint-disable-line no-unmodified-loop-condition
checkNumber(guess);
};
window.giveUp = function () {
exit('danger', `Number was ${number}.`);
};
function restart () {
lowerLimit = +document.getElementById('lowerLimit').value;
upperLimit = +document.getElementById('upperLimit').value;
guessLimit = +document.getElementById('guessLimit').value;
positions = document.getElementById('positions').checked;
biggerSmaller = document.getElementById('biggerSmaller').checked;
if (guessLimit === 0) {
guessLimit = Number.MAX_SAFE_INTEGER;
}
do {
number = Math.floor(Math.random() * (upperLimit - lowerLimit + 1) + lowerLimit);
} while (checkDigits(number));
digits = findDigitsCount(number);
sum = findDigitsSum(number);
updateSum();
guesses = 1;
oldGuesses = [];
window.locked = false;
guessLabel.innerHTML = guesses + '. Guess';
document.getElementById('text').innerHTML = '';
}
function checkNumber (guess) {
oldGuesses.push(guess);
if (number === guess) {
exit('success', number + ' is correct.');
} else {
let text = guess + ' is incorrect.';
if (positions) {
let corrects = 0;
let incorrects = 0;
const a = guess.toString();
const b = number.toString();
for (let i = 0; i < a.length; i++) {
if (a.charAt(i) === b.charAt(i)) {
corrects++;
}
for (let j = 0; j < b.length; j++) {
if (i !== j && a.charAt(i) === b.charAt(j)) {
incorrects++;
}
}
}
text += `<br>${corrects} in correct position, ${incorrects} in incorrect position, ${digits - corrects - incorrects} no match.`;
}
if (biggerSmaller) {
if (guess > number) {
text += '<br>Your guess should be smaller.';
} else {
text += '<br>Your guess should be larger.';
}
}
if (guesses === guessLimit) {
exit('danger', `Number was ${number}.`);
} else {
if (++guesses === guessLimit) {
guessLabel.innerHTML = 'Last Guess';
} else {
guessLabel.innerHTML = guesses + '. Guess';
}
createAlert('warning', text);
}
}
}
function checkDigits (n) {
return (/([0-9]).*?\1/).test(n);
}
function findDigitsCount (n) {
return n.toString().length;
}
function findDigitsSum (n) {
let sum = 0;
while (n > 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
function updateSum () {
if (sumCheck) {
document.getElementById('sum').innerHTML = sum;
} else {
document.getElementById('sum').innerHTML = '';
}
}