-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
246 lines (224 loc) · 7.07 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Get references to DOM elements
var nextNumber = document.getElementById("next-number");
var display = document.getElementById("display-pattern");
var result = document.getElementById("result");
var nextButton = document.getElementById("next-button");
var difficulty = document.getElementById("difficulty-level");
var hint = document.getElementById("hint");
var tries = 0;
var totalTries = 0;
var win = 0;
var numbers = [];
var randomFunctions = [
addSquares,
twiceAdded,
subtractCube,
cubedAdd,
fibonacci,
exponential_one,
exponential_two,
exponential_three,
exponential_four,
log_e,
];
var timer;
var timeLeft = 60;
var leaderboard = JSON.parse(localStorage.getItem("leaderboard")) || [];
// Function to generate a sequence based on squares
function addSquares() {
var randomFirst = Math.floor(Math.random() * 9) + 1;
numbers.push(randomFirst);
for (var i = 0; i < 5; i++) {
numbers.push(numbers[i] + Math.pow(i + 1, 2));
}
hint.innerHTML = "Think of squares";
difficulty.innerText = "Difficulty: Easy";
}
// Function to generate a sequence by adding twice the previous number plus the index
function twiceAdded() {
var randomFirst = Math.floor(Math.random() * 9) + 1;
numbers.push(randomFirst);
for (var i = 0; i < 5; i++) {
numbers.push(2 * numbers[i] + (i + 1));
}
hint.innerHTML = "Think of numbers doubled";
difficulty.innerText = "Difficulty: Easy";
}
// Function to generate a sequence with subtraction of cubes
function subtractCube() {
var randomFirst = Math.floor(Math.random() * 3) + 1;
for (i = randomFirst; i < randomFirst + 5; i++) {
numbers.push(Math.pow(i + 1, 3) - (i + 1));
}
hint.innerHTML = "Try raising to the 3rd power";
difficulty.innerText = "Difficulty: Medium";
}
// Function to generate a sequence by adding cubes and products
function cubedAdd() {
var randomFirst = Math.floor(Math.random() * 3) + 1;
for (i = randomFirst; i < randomFirst + 5; i++) {
numbers.push(Math.pow(i + 1, 3) + (i + 2) * (i + 3));
}
hint.innerHTML = "Think of cubes";
difficulty.innerText = "Difficulty: Medium";
}
// Function to generate a Fibonacci sequence
function fibonacci() {
var randomFirst = Math.floor(Math.random() * 9) + 1;
numbers.push(randomFirst);
for (var i = 0; i < 5; i++) {
if (i === 0) {
numbers.push(randomFirst + 0);
} else {
numbers.push(numbers[i - 1] + numbers[i]);
}
}
hint.innerHTML = "Think of Fibonacci sequence";
difficulty.innerText = "Difficulty: Easy";
}
// Function to generate a sequence with exponential growth
function exponential_one() {
var x = Math.floor(Math.random() * 20) + 1;
let term = (n) => Math.pow(1 + x / n, n);
for (let i = 0; i <= 5; i++) {
let num = toFixed(term(i), 2);
numbers.push(num);
}
hint.innerHTML = "What do you know about exponential series?";
difficulty.innerText = "Difficulty: Hard";
}
// Function to generate a sequence with negative powers
function exponential_two() {
var n = Math.floor(Math.random() * 5) + 1;
let term = (i) => Math.pow(-1, n) * Math.pow(i, n);
for (let i = 0; i < 5; i++) {
numbers.push(term(i));
}
hint.innerHTML = "Look again, think in powers";
difficulty.innerText = "Difficulty: Hard";
}
// Function to generate a sequence with a number raised to the index and divided by the index
function exponential_three() {
let randomnum = Math.floor(Math.random() * 10) + 1;
for (let i = 0; i < 5; i++) {
let power = i + 1;
let value = Math.pow(randomnum, power) / power;
let rounded = toFixed(value, 2);
numbers.push(rounded);
}
hint.innerHTML = "A number is raised to the index and divided by the index";
difficulty.innerText = "Difficulty: Hard";
}
// Function to generate a sequence with exponential decay
function exponential_four() {
let randomnum = Math.random() * 5 + 1;
for (let i = 0; i < 5; i++) {
let value = Math.exp(-1 / Math.pow(randomnum + i, 2));
let rounded = toFixed(value, 2);
numbers.push(rounded);
}
hint.innerHTML = "An exponential of a number";
difficulty.innerText = "Difficulty: Hard";
}
// Function to generate a sequence with logarithms
function log_e() {
let randomnum = Math.random() * 10 + 1;
for (let i = 0; i < 5; i++) {
let value = Math.log(randomnum + i);
numbers.push(toFixed(value, 2));
}
hint.innerHTML =
"Find out a random number added to index and try some log on it";
difficulty.innerText = "Difficulty: Hard";
}
// Function to round a number to a fixed number of decimal places
function toFixed(num, precision) {
return (+(Math.round(+(num + "e" + precision)) + "e" + -precision)).toFixed(
precision,
);
}
// Function to call a random pattern function from the array
function callRandomFunction() {
const randomIndex = Math.floor(Math.random() * randomFunctions.length);
randomFunctions[randomIndex]();
}
// Function to create and append a span element to the display
function createSpan(element) {
var span = document.createElement("span");
span.innerHTML = element;
display.appendChild(span);
}
// Function to display the pattern of numbers
function displayPattern() {
numbers.slice(0, -1).map((element) => createSpan(element));
}
// Function to handle the user's guess
function guess() {
tries++;
totalTries++;
document.getElementById("total-tries").innerHTML = totalTries;
if (tries == 3) {
nextButton.style.display = "inline-block";
}
if (nextNumber.value == numbers[numbers.length - 1]) {
win++;
document.getElementById("wins").innerHTML = win;
result.innerHTML = "You win!";
result.style.color = "#74d900";
hint.innerHTML = "";
setTimeout(function () {
nextPattern();
nextNumber.value = "";
}, 1000);
} else {
if (tries >= 3) {
hint.style.display = "block";
}
result.innerHTML = "Nope, try again";
result.style.color = "#d90074";
}
}
// Add event listener for Enter key press to submit guess
nextNumber.onkeypress = function (event) {
if (event.which == 13 || event.keyCode == 13) {
guess();
return false;
}
return true;
};
// Function to start a new pattern
function nextPattern() {
tries = 0;
result.innerHTML = "";
hint.style.display = "none";
document.getElementById("display-pattern").innerHTML = "";
numbers = [];
nextButton.style.display = "none";
callRandomFunction();
displayPattern();
startTimer();
}
//Hide How-To Instructions
document.getElementById("hide-instructions").onclick = function(e) {
e.preventDefault;
document.getElementById("how-to-container").classList.toggle("hidden")
}
// Function to start the timer
function startTimer() {
timeLeft = 60;
timer = setInterval(function () {
var minutes = Math.floor(timeLeft / 60);
var seconds = timeLeft % 60;
document.getElementById("timer").innerText =
`${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
timeLeft--;
if (timeLeft < 0) {
clearInterval(timer);
result.innerHTML = "Time's up!";
result.style.color = "#d90074";
nextButton.style.display = "inline-block";
}
}, 1000);
}
// Start the game with the first pattern
nextPattern();