-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
295 lines (229 loc) · 6.43 KB
/
script.js
File metadata and controls
295 lines (229 loc) · 6.43 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
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
'use strict';
// // strict ES6 to avoid bugs of earlier standarts of JS:
// a = 5;
// console.log(a);
// Primitives:
var number = 5;
var string = "Hello!";
var sym = Symbol(); // just in ES6
var boolean = true;
null;
undefined;
// Objects:
var object = {};
var array = [];
// let userName = prompt("What is your name?"),
// overAge = confirm("Are you over 18?");
// if (overAge === true) {
// let answer = +prompt("Are you over 21?", "Yes");
// // + makes the srting a number
// console.log(typeof(answer));
// };
// alert("Nice to meet you, " + userName);
// console.log("Also great to meet you, " + userName);
let incr = 10,
decr = 10;
incr++;
// prefix form of operators
console.log(incr);
console.log(--decr);
// postfix form of the decrement operator - returns the value first, then changes it
console.log(decr--);
// Operator Precedence (grouping -> incr,decr -> multipl,division,modulo -> addition,substraction):
let x = 1 + 2 - 3, // => left to right associativity (in general) 3-3
y = 1 + 2 * 3, // 1 + 6
z = (1 + 2) * 3; // 3 * 3
console.log("x,y,z:", x, y, z);
// Operator Associativity
// Assignment (right to left <= )
let a = 1,
b = 2,
c = 3;
console.log("a,b,c: before", a, b, c)
a = b = 3;
console.log("a,b,c:", a, b, c)
// Conditions:
let fruit = "Mangoes";
if (fruit === "Mangoes") {
console.log("Mangoes are $2.79 a pound.");
} else if (fruit === "Papayas") {
console.log("Papayas are $2.79 a pound.");
} else if (fruit === "Oranges") {
console.log("Oranges are $0.59 a pound.");
} else {
console.log("Sorry, we're out of " + fruit + ".");
}
// Ternary (conditional) Operator:
(fruit === "Mangoes") ? console.log("Mangoes are $2.79 a pound.") : console.log("Try again!");
// Switch Statement:
switch (fruit) {
case 'Oranges':
console.log('Oranges are $0.59 a pound.');
break;
case 'Mangoes':
case 'Papayas':
console.log('Mangoes and papayas are $2.79 a pound.');
// expected output: "Mangoes and papayas are $2.79 a pound."
break;
default:
console.log('Sorry, we are out of ' + fruit + '.');
}
//// With Block-Scope Variables (let & const):
const action = 'say_hello';
switch (action) {
case 'say_hello': { // added brackets
let message = 'hello';
console.log(message);
break;
} // added brackets
case 'say_hi': { // added brackets
let message = 'hi';
console.log(message);
break;
} // added brackets
default: { // added brackets
console.log('Empty action received.');
break;
} // added brackets
}
// Algorithms :
let num = 50;
//// While-Loop:
// while (num < 53) {
// console.log(num);
// num++;
// }
//// Do-Loop:
do {
console.log(num);
num++;
}
while (num < 53);
//// For-Loop:
for (let i = 0; i < 15; i++) {
if (i % 2 === 0) {
continue;
}
if (i === 11) {
break;
}
console.log(i);
}
// Arrow Functions (ECMAScript 6):
let calc = (a,b) => a+b;
// let calc = (a,b) => {a+b}; // for long expressions
console.log(calc(3,5));
// Methods:
let str = 'tEsT';
console.log(str.length);
console.log(str.toUpperCase());
console.log(str.toLowerCase());
let twelve = '12.3';
console.log(Math.round(twelve));
let eleven = '11.7px';
console.log(parseInt(eleven));
// Callback Functions:
function first(){
// do something
setTimeout( function(){
console.log('1) callback func');
}, 1000 );
}
function second(){
console.log('2) regular');
}
first();
second();
function learnJS(lang, callback) {
console.log("I'm learning " + lang);
callback();
}
// learnJS("JavaScript", function() {
// console.log("I finished one more lesson!");
// })
function theCallback() {
console.log("I finished one more lesson!");
}
learnJS("JavaScript", theCallback)
// Objects
//// first way:
let oldSchool = new Object();
//// second way (best practice):
let options = {
width: 1024,
height: 1024,
name: "test"
};
console.log(options.name + " for Objects");
// add key-value & nest objects:
options.bool = false;
options.colors = {
border: "black",
bg: "red"
};
// delete:
delete options.bool;
console.log(options);
// Iterate data in object using For-In operator:
// Перебрать данные в объекте с оператором For-In:
for (let key in options) {
console.log("Key " + key + " has a value " + options[key]);
}
//// Number of keys/свойств within object:
console.log(Object.keys(options).length);
//// functions applied to objects called "methods"
// Arrays / массивы
let arr = [1, "two", 3, "four", 5];
arr.pop(); // delete last element;
arr.push("5"); // add last element (as string)
arr.shift(); // delete 1st element;
arr.unshift("1"); // add 1st element (as string)
// Method for-loop:
// for (let i = 0; i < arr.length; i++) {
// console.log(arr[i]);
// }
// Method forEach:
arr.forEach(function(item, i, mass) {
console.log(i + ": " + item + " (array: " + mass + ")");
})
console.log(arr);
// Method for-of (ES6 - NOT for objects, works JUST with arrays, strings and some new types of obj from ES6 like maps):
let mass = [1,3,4,6,7];
//// for-in will return keys of items:
for (let key in mass) {
console.log(key + " is a key returned with for-in");
}
//// for-of will return items themselves:
for (let key of mass) {
console.log(key + " is an item returned with for-of");
}
// Method split - to return a srting as array with items
let ans = "one two three 1 4 3 15 fifteen",
// let ans = prompt("Write some words with spaces", ""),
words = [];
words = ans.split(" "); // space as separator to split a line
console.log("Array (.split): ");
console.log(words);
// Method join - to return array as a string
let newString = words.join(", "); // comma as separator for words in a string
console.log("String (.join): ");
console.log(newString);
// Method sort - to sort items in alphabetic order:
// let i = words.sort(); // sorts: "1, 15, 1020, 2, 3, ..., a, b, c, ..., z"
let i = words.sort(); // sorts better with numbers and strings: "1, 2, 3, 15, 1020, ..., a, b, c, ..., z"
function compareNum(a,b) {
return a-b;
}
console.log("Sorted (.sort): ");
console.log(words);
// Object-Oriented Programming (prototyping)
let soldier = {
health: 400,
armor: 200
}
let john = {
health: 100
}
john.__proto__ = soldier;
console.log(john); // just original data of obj => { health: 100 }
console.log(john.armor); // will keep searching in prototypes if none found in original obj => 200