-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
221 lines (185 loc) · 5.24 KB
/
app.js
File metadata and controls
221 lines (185 loc) · 5.24 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
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
'use strict';
/* DRAW FIGURES WITH CYCLES */
function drawFigure() {
let types = [
'rectangle',
'right triangle',
'regular triangle',
'diamond'
];
let errorMessage = "Incorrect enter. Please try again";
let greeting = `What do you want to draw? (Enter the number)
1: ${types[0]}
2: ${types[1]}
3: ${types[2]}
4: ${types[3]}`;
let type,
width,
height;
while (true) {
type = prompt(greeting);
if (type > 0 && type <= types.length) break;
alert(errorMessage);
};
width = userInput("Enter the figure's width (integer)", errorMessage);
height = userInput("Enter the figure's height (integer)", errorMessage);
console.log(`I'll draw the ${types[type - 1]} with ${width} width and ${height} height`);
console.log(getFigure(type, width, height));
function userInput(message, errorMessage) {
let input;
while (true) {
input = prompt(message);
if ( Number.isInteger(+input) && input > 0 ) break;
alert(errorMessage);
};
return input;
};
function getFigure(type, width, height) {
// Rectangle
if (type == 1) {
let figure = "";
for (let i = 0; i < height; i++) {
let row = "";
for (let j = 0; j < width; j++) {
row += "*";
}
figure += row + "\n";
}
return figure;
}
// Right Triangle
if (type == 2) {
let row = '*';
let figure = '';
for (let i = 0; i < width; i++) {
figure += (row + '\n');
row += '*';
}
console.log('Height is ignored');
return figure;
};
};
};
addSnippetOnPage(drawFigure);
/* SHIFTED DIFFERENT */
function shiftedDiff(first, second) {
let count = 0;
for (let i = 0; i < first.length; i++) {
if (first == second) {
return count;
}
first = first.slice(-1) + first.slice(0, -1);
count++;
}
return -1;
}
addSnippetOnPage(shiftedDiff);
/* FORMAT PHONE NUMBER */
function createPhoneNumber(numbers) {
let formatChars = { "0": "(", "4": ")", "5": " ", "9": "-" };
for (let key in formatChars) {
numbers.splice(key, 0, formatChars[key]);
}
return numbers.join("");
}
addSnippetOnPage(createPhoneNumber);
/* FIBONACCI with RECURSION */
function getFibonacciRecursion(n) {
if (n === 1) {
return [0, 1];
}
let fib = getFibonacciFor(n - 1),
len = fib.length;
fib.push(fib[len - 1] + fib[len - 2]);
return fib;
}
addSnippetOnPage(getFibonacciRecursion);
/* FIBONACCI with CYCLE */
function getFibonacciFor(n) {
let fib = [0, 1];
for (i = 0; i < n - 1; i++) {
let len = fib.length;
fib.push(fib[len - 1] + fib[len - 2]);
}
return fib.slice(1);
}
addSnippetOnPage(getFibonacciFor);
/* CHECK BRACKETS BALANCE */
function checkBrackets(str) {
let open = {
"(": ")",
"[": "]",
"<": ">",
};
let close = {
")": true,
"]": true,
">": true,
};
let stack = [];
for (i = 0; i < str.length; i++) {
let char = str[i];
if (open[char]) {
stack.push(char);
} else if (close[char]) {
if (open[stack.pop()] != char) {
return 0;
}
}
}
return stack.length == 0 ? 1 : 0;
}
addSnippetOnPage(checkBrackets);
/* NOTADAPTIVE FUNC */
function notGoodCode(s, a, b) {
var match_empty = /^$/;
if (s.match(match_empty)) {
return -1;
} else {
var i = s.length - 1;
var aIndex = -1;
var bIndex = -1;
while (aIndex == -1 && bIndex == -1 && i >= 0) {
if (s.substring(i, i + 1) == a) aIndex = i;
if (s.substring(i, i + 1) == b) bIndex = i;
i--;
}
if (aIndex != -1) {
if (bIndex == -1) return aIndex;
else return Math.max(aIndex, bIndex);
} else {
if (bIndex != -1) return bIndex;
else return -1;
}
}
}
addSnippetOnPage(notGoodCode);
function itsGoodCode(s, a, b) {
if (!s.length) {
return -1;
}
return s.indexOf(a) > s.indexOf(b) ? s.indexOf(a) : s.indexOf(b);
}
addSnippetOnPage(itsGoodCode);
/* GOOD VS EVIL */
function goodVsEvil(good, evil) {
let goodWorth = [1, 2, 3, 3, 4, 10],
evilWorth = [1, 2, 2, 2, 3, 5, 10],
wins = {
"1": "Battle Result: Good triumphs over Evil",
"-1": "Battle Result: Evil eradicates all trace of Good",
"0": "Battle Result: No victor on this battle field",
};
function powerIt(amount, worth) {
let arr = amount.split(" ").map((item, i) => item * worth[i]);
return arr.reduce((accum, next) => (accum += next));
}
let result = Math.sign(powerIt(good, goodWorth) - powerIt(evil, evilWorth));
return wins[result];
}
addSnippetOnPage(goodVsEvil);
// add snippets from main.js
addSnippetOnPage(addSnippetOnPage);
addSnippetOnPage(toggleVisibility);
addSnippetOnPage(textToHide);
addSnippetOnPage(createNode);