-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
255 lines (248 loc) · 7.26 KB
/
script.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
247
248
249
250
251
252
253
254
255
"use strict";
/*const arr = [2, 3, 4];
const a = arr[0];
const b = arr[1];
const c = arr[2];
const [x, y, z] = arr;
console.log(x, y, z);
console.log(arr);
let [main, , secondary] = restaurant.categories;
console.log(main, secondary);
// Switching with an auxiliary variable
// const temp = main;
// main = secondary;
// secondary = temp;
// console.log(main, secondary);
[main, secondary] = [secondary, main];
console.log(main, secondary);
//Recieve return values from a function :)
const [starter, mainCourse] = restaurant.order(2, 0);
console.log(starter, mainCourse);
///Nested
const nested = [2, 4, [5, 6]];
const [i, , [j, k]] = nested;
console.log(i, j, k);
///Default
const [p = 1, q = 1, r = 1] = [8];
console.log(p, q, r); */
/*
console.log(restaurantName, hours, tags);
///Default
const { menu = [], starterMenu: starters = [] } = restaurant;
console.log(menu, starters);
// Mutating variables
let a = 11;
let b = 99;
const obj = { a: 23, b: 7, c: 14 };
({ a, b } = obj);
console.log(a, b);
///Nested objects
const {
fri: { open: o, close: c },
} = openingHours;
console.log(o, c);
*/
// Data needed for a later exercise
const flights =
"_Delayed_Departure;fao93766109;txl2133758440;11:25+_Arrival;bru0943384722;fao93766109;11:45+_Delayed_Arrival;hel7439299980;fao93766109;12:05+_Departure;fao93766109;lis2323639855;12:30";
// Data needed for first part of the section
const restaurant = {
name: "Classico Italiano",
location: "Via Angelo Tavanti 23, Firenze, Italy",
categories: ["Italian", "Pizzeria", "Vegetarian", "Organic"],
starterMenu: ["Focaccia", "Bruschetta", "Garlic Bread", "Caprese Salad"],
mainMenu: ["Pizza", "Pasta", "Risotto"],
order: function (starterIndex, mainIndex) {
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
},
openingHours: {
thu: {
open: 12,
close: 22,
},
fri: { open: 11, close: 23 },
sat: {
open: 0,
close: 24,
},
},
orderDelivery: function ({
starterIndex = 1,
mainIndex = 0,
time = "20:00",
adress,
}) {
console.log(
`Order recieved: ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered to ${adress} at ${time}`
);
},
orderPasta: function (ing1, ing2, ing3) {
console.log(`Here's some pasta with ${ing1}, ${ing2}, ${ing3}`);
},
};
// restaurant.orderDelivery({
// time: "22:30",
// adress: "Via del Sole, 21",
// mainIndex: 2,
// starterIndex: 2,
// });
// restaurant.orderDelivery({ adress: "Via del Sole, 21", starterIndex: 1 });
// const { name, openingHours, categories } = restaurant;
// const {
// name: restaurantName,
// openingHours: hours,
// categories: tags,
// } = restaurant;
// const arr = [7, 8, 9];
// const badNewArr = [1, 2, arr[0], arr[1], arr[2]];
// console.log(badNewArr);
// const newArr = [1, 2, ...arr];
// console.log(newArr);
// console.log(...newArr);
// const newMenu = [...restaurant.mainMenu, "Gnocci"];
// console.log(newMenu);
// ///Copy array
// const mainMenuCopy = [...restaurant.mainMenu];
// console.log(mainMenuCopy);
// ///Join 2 arrays
// const menu = [...restaurant.mainMenu, ...restaurant.starterMenu];
// console.log(menu);
// ///Iterables are arrays, strings, maps and sets !objects
// const str = "Jonas";
// const letters = [...str, "S."];
// console.log(letters);
// console.log(...str);
// const ingredients = [
// prompt("Let's make pasta! Number 1:"),
// prompt("Number 2:"),
// prompt("Number 3:"),
// ];
// console.log(ingredients);
// restaurant.orderPasta(ingredients[0], ingredients[1], ingredients[2]);
// restaurant.orderPasta(...ingredients);
///Objects
// const newRestaurant = { foundingYear: 1998, ...restaurant, founder: "Jaxtin" };
// console.log(newRestaurant);
// newRestaurant.mainMenu = ["New Shit"];
// console.log(restaurant.mainMenu);
// const restaurantCopy = { ...restaurant };
// restaurantCopy.name = "Ristorante Roma";
// console.log(restaurantCopy.name);
// console.log(restaurant.name);
//////////////////////////////////////////////
let colors = [
"darkblue",
"darkcyan",
"darkgoldenrod",
"darkgray",
"darkgreen",
"darkgrey",
"darkkhaki",
"darkmagenta",
"darkolivegreen",
"darkorange",
"darkorchid",
"darkred",
"darksalmon",
"darkseagreen",
"darkslateblue",
"darkslategray",
"darkslategrey",
"darkturquoise",
"darkviolet",
"deeppink",
"deepskyblue",
"dimgray",
"dimgrey",
"dodgerblue",
"firebrick",
"floralwhite",
"forestgreen",
"gainsboro",
"ghostwhite",
"gold",
"goldenrod",
"greenyellow",
"grey",
"honeydew",
"hotpink",
"indianred",
"indigo",
"ivory",
"khaki",
"lavender",
"lavenderblush",
"lawngreen",
"lemonchiffon",
"lightblue",
"lightcoral",
"lightcyan",
];
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function setCharAt(str, index, chr) {
if (index > str.length - 1) return str;
return str.substring(0, index) + chr + str.substring(index + 1);
}
let isUpper = (char) => {
if (char >= 97 && char <= 97 + 32) return 0;
if (char >= 65 && char <= 65 + 32) return 1;
return 2;
};
let userText = prompt(`Input your text here and watch magic happen!`);
let perm = document.querySelector("#permutation");
let pace = prompt(
`What pace do you want for your text(miliseconds)? ${userText.length} characters.`
);
perm.textContent = userText;
console.log(perm.textContent);
let randomColorIndex;
let maximumColorIndex = colors.length - 1;
perm.addEventListener("mouseover", async function () {
let cnt = 0;
let aux = perm.textContent.split("");
while (cnt < perm.textContent.length) {
randomColorIndex = Math.floor(Math.random() * maximumColorIndex);
perm.style.color = colors[randomColorIndex];
let currentChar = aux[cnt].charCodeAt(0);
if (
(currentChar >= 65 && currentChar <= 65 + 32) ||
(currentChar >= 97 && currentChar <= 122)
) {
if (isUpper(currentChar)) {
while (currentChar < "X".charCodeAt(0)) {
currentChar++;
aux[cnt] = String.fromCharCode(currentChar);
perm.textContent = setCharAt(perm.textContent, cnt, aux[cnt]);
await sleep(pace);
}
} else {
while (currentChar < "x".charCodeAt(0)) {
currentChar++;
aux[cnt] = String.fromCharCode(currentChar);
perm.textContent = setCharAt(perm.textContent, cnt, aux[cnt]);
await sleep(pace);
}
}
if (currentChar > "x".charCodeAt(0)) {
while (currentChar > "x".charCodeAt(0)) {
currentChar--;
aux[cnt] = String.fromCharCode(currentChar);
perm.textContent = setCharAt(perm.textContent, cnt, aux[cnt]);
await sleep(pace);
}
} else if (isUpper(currentChar) && currentChar > "X".charCodeAt(0)) {
while (currentChar > "X".charCodeAt(0)) {
currentChar--;
aux[cnt] = String.fromCharCode(currentChar);
perm.textContent = setCharAt(perm.textContent, cnt, aux[cnt]);
await sleep(pace);
}
}
cnt++;
await sleep(pace);
} else cnt++;
}
});
console.log("Y".charCodeAt(0));