-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
311 lines (262 loc) · 10.7 KB
/
main.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// const generatedContent = document.getElementById('body')
function mainPageContent() {
const renderMainPageContent =
`
<main id="main">
<a href="https://github.com/Hacking-NASSA-with-HTML/javascript-notebook" target="_blank"><img width="149"
height="149" src="./forkme_left_red.png" style="position: fixed; top:0; left:0" alt="Fork me on GitHub">
</a>
<article class="article">
<div class="article--header">
<h2>How to deploy a piece of code as text on site:</h2>
</div>
<div class="article--headline">
<p>see results in the console also</p>
<p>the code is generated by JavaScript</p>
<p>HTML code:</p>
</div>
<div class="highlight--piece--of--code">
<pre>
<code>
<div class="highlight--piece--of--code">
<pre>
<code>
****************************** Your code here *******************************************************************
</code>
</pre>
</div>
</code>
</pre>
</div>
<p>CSS code:</p>
<div class="highlight--piece--of--code">
<pre>
<code>
.highlight--piece--of--code {
background-color: #08090a;
color: #f8f8f3;
border-radius: 5px;
overflow-x: auto;
}
</code>
</pre>
</div>
</article>
<article class="article">
<div class="article--header">
<h2>Zhan Zhuridov's Observer. Initial variant:</h2>
</div>
<div class="article--headline">
<p>The second variant is in my GHgists:</p>
<p><a href="https://gist.github.com/Hacking-NASSA-with-HTML/60367e3fbeb7140a6983620cffc6a1f2"
target="_blank">https://gist.github.com/Hacking-NASSA-with-HTML/60367e3fbeb7140a6983620cffc6a1f2</a>
</p>
</div>
<div class="highlight--piece--of--code">
<pre>
<code>
class Observer{
constructor(){
this.listeners = [];
}
addListener(name, callback){
let id = {};
this.listeners.push({id, name, callback});
return id;
}
addOnceListener(name, callback){
let id = {};
this.listeners.push({id, name, callback:()=>{
callback();
this.removeListener(id);
}
});
return id;
}
removeListener(id){
this.listeners = this.listeners.filter(it=>it.id!=id);
}
dispatch(name){
this.listeners.filter(it=>it.name==name).forEach(it=>it.callback());
}
}
</code>
</pre>
</div>
</article>
<div class="article--header article--headline">
<h2>Did the Promise finish?</h2>
<p class="completion">Not yet</p>
</div>
</main>
`
return renderMainPageContent
}
document.body.insertAdjacentHTML("afterend", mainPageContent())
// .map with arrow function
const upperizedNames = ['Farrin', 'Kagure', 'Asser'].map(
name => name.toUpperCase()
)
console.log(upperizedNames)
// .filter with ordinary and arrow functions
const names = ['Afghanistan', 'Aruba', 'Bahamas', 'Chile', 'Fiji', 'Gabon', 'Luxembourg', 'Nepal', 'Singapore', 'Uganda', 'Zimbabwe'];
const longNames = names.filter(function (name) {
return name.length > 6
})
const longNamesArrow = names.filter(name => name.length > 6)
console.log(longNames)
console.log(longNamesArrow)
// example of arrow .map()
const squares = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(
square => square * square
)
console.log(...squares) // works without ... too. Why?
// default function parameters:
function hello(name = 'Visitor', greeting = 'Welcome') {
return `${greeting} ${name}!`
}
console.log(hello()) // Welcome Visitor!
console.log(hello('Nikky')) // Welcome Nikky!
console.log(hello('Jennifer', 'Howdy')) // Howdy Jennifer!
// one more example of default function parameters:
function houseDescriptor([houseColor = 'green', shutterColors = ['red']]) {
return `I have a ${houseColor} house with ${shutterColors.join(' and ')} shutters`
}
console.log(houseDescriptor(['green']))
console.log(houseDescriptor(['green', ['white', 'gray', 'pink']]))
// one more example of default function parameters:
function buildHouse({ floors = 1, color = 'red', walls = 'brick' } = {}) {
return `Your house has ${floors} floor(s) with ${color} ${walls} walls.`
}
console.log(buildHouse()) // Your house has 1 floor(s) with red brick walls.
console.log(buildHouse({})) // Your house has 1 floor(s) with red brick walls.
console.log(buildHouse({ floors: 3, color: 'yellow' })) // Your house has 3 floor(s) with yellow brick walls.
// example of es6 classes and subclasses syntax
class Vehicle {
constructor(color = 'blue', wheels = 4, horn = 'beep beep') {
this.color = color
this.wheels = wheels
this.horn = horn
}
honkHorn() {
console.log(this.horn)
}
}
class Bicycle extends Vehicle {
constructor(color = 'blue', wheels = 4, horn = 'beep beep') {
super(color, wheels, horn)
this.wheels = 2
this.horn = 'honk honk'
}
honkHorn() {
console.log(this.horn)
}
}
const myVehicle = new Vehicle()
myVehicle.honkHorn() // beep beep
const myBike = new Bicycle()
myBike.honkHorn() // honk honk
// For...of loop!!!!!! MOST COOL for Loop !!!!!
const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (const digit of digits) {
console.log(digit);
} // prints 0 1 2 3 4 5 6 7 8 9
// How to Create a Set:
const myFavoriteFlavors = new Set()
myFavoriteFlavors.add("chocolate chip")
myFavoriteFlavors.add("cookies and cream")
myFavoriteFlavors.add("strawberry")
myFavoriteFlavors.add("vanilla")
myFavoriteFlavors.delete("strawberry")
console.log(myFavoriteFlavors)
// How to Create a WeakSet:
const uniqueFlavors = new WeakSet()
const flavor1 = { flavor: 'chocolate' }
const flavor2 = { flavor: 'orange' }
uniqueFlavors.add(flavor1)
uniqueFlavors.add(flavor2)
console.log(uniqueFlavors)
// How to Create a Promise:
new Promise(function (resolve) {
console.log('first')
resolve()
console.log('second')
}).then(function () {
console.log('third')
}) // prints first second third
// Promise Example in HTML rendering:
let milliseconds = 3000
wait(milliseconds).then(finish)
function wait(ms) {
return new Promise(function (resolve) {
console.log(this)
window.setTimeout(function () {
resolve()
}, ms)
})
}
function finish() {
let completion = document.querySelector('.completion')
completion.innerText = "Completed after " + milliseconds
+ " ms."
} // prints 'Completed after 3000 ms.' in HTML rendering
// instead of 'Not yet' in <p class="completion">Not yet</p>
// after 3 seconds
// Proxies syntax:
const proxyObj = new Proxy({ age: 5, height: 4 }, {
get(targetObj, property) {
console.log(`getting the ${property} property`);
console.log(targetObj[property]);
}
});
proxyObj.age; // logs 'getting the age property' & 5
proxyObj.height; // logs 'getting the height property' & 4
proxyObj.weight = 120; // set a new property on the object
proxyObj.weight; // logs 'getting the weight property' & 120
// Destructuring of the array syntax:
const things = ['red', 'basketball', 'paperclip', 'green', 'computer', 'earth', 'blue', 'dogs']
const [one, , , two, , , , three] = things
console.log(one, two, three) // prints red green dogs
// How to use the Spread operator syntax:
const fruits = ["apples", "bananas", "pears"]
const vegetables = ["corn", "potatoes", "carrots"]
const produce = [...fruits, ...vegetables]
// prints ['apples', 'bananas', 'pears', 'corn', 'potatoes', 'carrots']
console.log(produce)
// How to use the Rest operator syntax:
const order = [20.17, 18.67, 1.50, "cheese", "eggs", "milk", "bread"]
const [total, subtotal, tax, ...items] = order
// prints 20.17 18.67 1.5 ['cheese', 'eggs', 'milk', 'bread']
console.log(total, subtotal, tax, items)
// How to use different special characters inside String:
console.log("Up up\n\tdown down") // \n newline \t tab \" '' (double quote)
// prints: Up up
// prints: down down // \\ \ (backslash)
// How to write Ternary Operator syntax:
// conditional ? (if condition is true) : (if condition is false)
let isWinter = true
let bgColor = isWinter ? 'white' : 'grey'
console.log(bgColor) // prints white
// Complex Ternary Operator syntax:
let eatsPlants = false
let eatsAnimals = true
let kind = eatsPlants
? (eatsAnimals ? "omnivore" : "herbivore")
: (eatsAnimals ? "carnivore" : "undefined")
console.log(kind) // prints carnivore
// How to write a Break Statement syntax:
let yourChoice = 2
switch (yourChoice) {
case 1:
console.log("You choose 1.")
break
case 2:
console.log("You choose 2.")
break
case 3:
console.log("You choose 3.")
break
default:
console.log("something went wrong")
} // prints You choose 2.
// prints something went wrong if yourChoice = 4