-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaScript_BasicCode
More file actions
500 lines (409 loc) · 11.6 KB
/
JavaScript_BasicCode
File metadata and controls
500 lines (409 loc) · 11.6 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*Author: Sword Lord of the Lonely Peak
*Date: 2020/05/29
*/
/*ECMAScript specification.
*Javascript is an intepreted language.
*Java is a compiled language. Thus Java and Javascript are not the same.
*Now for variables:
*=> const means that the variable value cannot be reassign.
*=> Only const if you know that you will need not need to
* reassign the variable.
*/
/*Variables*/
console.log('Hello World!');
console.log('Good Morning!');
let word1 = 'Dylan';
let word2 = 'LOL';
let num1 = 2;
let num2 = 3;
let isCool = true;
let isCool2 = false;
const fullName = `${num1 + num2} ${word2}`;
let example = 'Hello\n' + 'World';
let example2 = `${word1} ${word2}`;
console.log(example2);
console.log(example);
let name = 'DEATHLY';
console.log(name);
let firstName = 'MOSH', secondName = 'FALLEN', thirdName = 'JIEN';
let example3 = `${firstName} ${secondName} ${thirdName}`;
let exNum = 20;
let age = 30;
let x = null;
const x1 = null;
const x2 = 4.5;
const y = undefined;
let z;
console.log(typeof firstName); //testing type!
console.log(age);
console.log(example3);
console.log(isCool);
/*concatenation*/
console.log("My name is " + firstName + " and my age is " + age);
console.log(`My name is ${firstName} ${secondName} and my age is ${age}`);
let hello = `My name is ${firstName} ${secondName} and my age is ${age}`;
const s = 'technology, computers, it, code';
console.log(hello);
console.log(hello.length);
console.log(hello.toUpperCase());
console.log(hello.toLowerCase());
console.log(hello.substring(0,23).toUpperCase());
console.log(hello.split(''));
console.log(s.split(', '));
/*Arrays - variables that hold multiple values*/
const numbers = new Array(1,2,3,4,5);
const fruits = ['apples', 'oranges', 'pears', true, 10];
console.log(fruits);
console.log(numbers);
fruits[5] = 'grapes';
fruits.push('mangos'); //adds mangos to the end.
fruits.unshift('strawberries');
fruits.pop();
console.log(fruits);
console.log(fruits[0]);
console.log(Array.isArray(fruits));
console.log(fruits.indexOf('oranges'));
/*Object literals*/
const person = {
firstName1: 'John',
lastName1: 'Doe',
age: 30,
hobbies: ['music', 'movies', 'sports'],
address: {
street:'50 main st',
city: 'Boston',
state: 'MA'
}
}
console.log(person.firstName1, person.lastName1);
console.log(person.hobbies[1]);
console.log(person.address.city);
const { firstName1, lastName1, address: {city} } = person;
console.log(firstName1, city);
person.email = '[email protected]'; // adding properties.
console.log(person);
/*Array of objects*/
const todos = [{
id: 1,
text: 'Take out trash',
isCompleted: true
},
{
id: 2,
text: 'Meeting with Boss',
isCompleted: true
},
{
id: 3,
text: 'Dentist appointment',
isCompleted: false
}];
console.log(todos);
console.log(todos[1].text); //Gets the text found in index 1.
console.log(todos[2].text); //Gets the text found in index 2.
//This is how to send data to a server!
const todoJSON = JSON.stringify(todos); //This is to make array to JSON format.
console.log(todoJSON);
/*Loop*/
for(let i = 0; i <= 10; i++){
console.log(`For LOOP number: ${i}`);
}
console.log('\n');
let i = 0;
while(i<10)
{
console.log(`For LOOP number: ${i}`);
i++;
}
console.log('\n');
for(let j = 0; j < todos.length; j++)
{
console.log(todos[j].text);
}
console.log('\n');
for(let todo of todos)
{
console.log(todo.text);
}
/*forEach, map, filter*/
console.log('\n');
todos.forEach(function(todo){
console.log(todo.text);
});
console.log('\n');
const todoText = todos.map(function(todo){
return todo.text;
});
console.log(todoText);
console.log('\n');
const todoCompleted = todos.filter(function(todo){
return todo.isCompleted == true;
}).map(function(todo){
return todo.text;
});
console.log(todoCompleted);
/*Conditions*/
const x3 = 1;
const y3 = 21;
if(x3 === 10 && y3 > 10) // === matches the datatypes and the value.
{
console.log('x is 10');
}
else if(x3 > 10)
{
console.log('x is greater than 10');
}
else
{
console.log('x is less than 10');
}
if(x3 > 5 || y3 > 10) // === matches the datatypes and the value.
{
console.log('y greater 10 and x is greater than 5');
}
/*Ternary Operator*/
let color = x > 10 ? 'red' : 'blue';
console.log(color);
(color === 'red') ? console.log('The color is red') : console.log('The color is not red');
/*Switches*/
color = 'green';
switch(color)
{
case 'red':
console.log('The color is red!');
break;
case 'blue':
console.log('The color is blue!');
break;
default:
console.log('The color is not red or blue!');
break;
}
/*Functions*/
function addNums(num1 = 1, num2 = 1){
return (num1 + num2);
}
console.log(addNums(5, 45));
function addNums2(num1, num2){
console.log(num1 + num2);
}
addNums2(5, 45);
const addNums3 = (num1 = 1, num2 = 1) => { //Functions using arrow function (introducde in ES6)
return num1 + num2;
}
console.log(addNums3(5,45));
const addNums4 = (num1 = 1, num2 = 1) => num1 + num2;
console.log(addNums4(5,45));
todos.forEach(todo => console.log('Hello'));
/*Object Oriented Programming*/
//Constructor Function
//function Person(firstName, lastName, dob) {
//this.firstName = firstName;
//this.lastName = lastName;
//this.dob = new Date(dob);
//this.getBirthYear = function(){
// return this.dob.getFullYear();
//}
//this.getFullName = function(){
// return `${this.firstName} ${this.lastName}`;
//}
//}
//Person.prototype.getBirthYear = function() { //Prototype!
//return this.dob.getFullYear();
//}
//Person.prototype.getFullName = function() {
//return `${this.firstName} ${this.lastName}`;
//}
class Person {
constructor(firstName, lastName, dob)
{
this.firstName = firstName;
this.lastName = lastName;
this.dob = new Date(dob);
}
getBirthYear(){
return this.dob.getFullYear();
}
getFullName(){
return `${this.firstName} ${this.lastName}`;
}
}
//Instantiate Object
const person1 = new Person('Nick', 'Doe', '4-3-1980');
const person2 = new Person('Mary', 'Smith', '3-6-1970');
console.log(person1.firstName, person1.lastName, 'is date of birth is', person1.dob);
console.log(person2.firstName, person2.lastName, 'is date of birth is', person2.dob);
console.log(person1);
console.log(person1.getFullName());
console.log(person1.getBirthYear());
console.log(person2);
console.log(person2.getFullName());
console.log(person2.getBirthYear());
/* % => Remainder*/
let remainder = 11 % 3;
if(remainder % 2 == 0)
console.log('Even');
else if(remainder % 2 == 1)
console.log('Odd');
else
console.log('Number is neither odd or even!');
/*Escape Sequences in String*/
/*****
* \' single quote
* \" double quote
* \\ backslash
* \n newline
* \r carriage return
* \t tab
* \b backspace
* \f form feed
****/
var myString = '\'\"FirstLine\" \n \\ is shown \t tab \b backspace \f form feed\'';
console.log(myString);
/*Strings*/
var firstNameString = 'Ava';
var firstLetter = firstNameString[0];
console.log(firstLetter + ` is the first letter of the name ${firstNameString}!`);
var lastNameString = 'Herrman';
var lastNameIsLastLetter = lastNameString[lastNameString.length - 1]; // -1 is to get correct index of the last letter!
console.log(`The last letter of the name ${lastNameString} is ${lastNameIsLastLetter}!`);
/*Small Game*/
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb)
{
var result = "";
result += "The" + " " + myAdjective + " " + myNoun + " " + myVerb + " to the store " + myAdverb + ".";
return result;
}
console.log(wordBlanks("dog", "big", "ran", "quickly"));
/*Arrays*/
var myArray2 = [
{
id1: 12,
isCompletedYet: true,
isDeathYet: false,
Name: "Gustave Rederick"
},
{
id1: 13,
isCompletedYet: true,
isDeathYet: true,
Name: "Gray Robbins"
},
{
id1: 14,
isCompletedYet: true,
isDeathYet: false,
Name: "June Green"
}
];
let myArray3 = [["Hello", "20"]];
console.log(myArray2[0].Name + " status is he dead yet? " + myArray2[0].isDeathYet);
console.log(myArray2[1].Name + " status is he dead yet? " + myArray2[1].isDeathYet);
console.log(myArray2[2].Name + " status is he dead yet? " + myArray2[2].isDeathYet);
console.log(myArray3[0][0]);
myArray3.push(["Death", "22"]); //Pushes the value at the end of the array.
console.log(myArray3);
myArray3.unshift(["Strawberries", "20"]); //Pushes element to the beginning of the array.
myArray3.pop(); //Removes last element in an array.
console.log(myArray3);
const a1 = [1,2,3,4]
const a2 = [1,'two', 3, null];
const a3 = [
"What the hammer?",
"In what furnace was thy brain?",
"What the anvil? What dread grasp",
"Dare its deadly terrors clasp?",
];
const a4 = [
{name: "Ruby", hardness: 9},
{name: "Diamond", hardness: 10},
{name: "Topaz", hardness: 8},
];
console.log(a3[0]);
console.log(a4[0].name, a4[1].name, a4[2].name);
console.log(a2[3]);
/*Dates*/
const halloweenParty = new Date(2020, 9, 31, 19, 0);
console.log(halloweenParty.getFullYear());
console.log(halloweenParty.getMonth());
console.log(halloweenParty.getDate());
console.log(halloweenParty.getHours());
console.log(halloweenParty.getMinutes());
console.log(halloweenParty.getSeconds());
console.log(halloweenParty.getMilliseconds());
/*Converting to Numbers*/
const numStr = "33.3"
const num = Number(numStr);
console.log("The number is " + num + "!");
const numConv = parseFloat("15.5 volts") //Always assume base 10!
const numConv2 = parseInt("3a", 16); //Hexadecimal, thus parse int at base 16.
const numConv3 = parseInt ("16 volts", 10); //parse int at base 10.
console.log(`Number 1 is ${numConv}!`);
console.log(`Number 2 is ${numConv2}!`);
console.log(`Number 3 is ${numConv3}!`);
const d = new Date();
const ts = d.valueOf(); //value of the date in miliseconds.
console.log(ts);
/*Converting Boolean to 1 or 0*/
const b = true;
const n = b ? 1 : 0;
console.log("The numerical value of the boolean variable b is " + n);
/*Converting to String*/
const n2 = 33.5;
const s2 = n2.toString();
console.log("The value of the number is " + n2 + " and its converted value is " + s2);
const arr = [1,true,"Hello"];
console.log(arr.toString()); //Converting array to string!
/*Converting to Boolean*/
const n3 = 0;
const b1 = !!n3; //Converting numbers to boolean!
const b2 = Boolean(n3); //Converting numbers to boolean!
console.log(n3);
console.log(b1);
console.log(b2);
/*Expressions*/
let x4, y4;
x4 = 3*5;
if(x4 == 15)
x4 = y4 = 4*5 + 5*4;
else
x4 = y4 = 15;
console.log(x4.toString());
console.log(y4.toString());
/****
* ^^Arithmetic Operators^^
* a+b addition (also for string concatenation)
* a-b subtraction
* a/b division
* a*b multiplication
* a%b remainder
* -a unary negation
* +a unary plus
* ++a pre-increment
* a++ post-increment
* --a pre-increment
* a-- post-increment
*****/
const x5 = 5;
const y5 = 3 - -x5;
console.log(y5.toString());
const x6 = 6;
const y6 = 8 + -x6;
console.log(y6.toString());
const x7 = 6;
const y7 = 8 + +x7;
console.log(y7.toString());
const s5 = "5";
let w5 = Number(s5);
w5++;
console.log(w5);
var a5 = 4;
(a5%2==0) ? console.log("Even") : console.log("Odd");
a5++;
(a5%10==0) ? console.log("Factor of 10!") : console.log("NOT a factor of 10!");
//Unary numbers in multiplication!
const x9=0, x10=3, x11=-1.5, x12=-6.33;
const p1 = -x9*1; //-9
const p2 = +x10*2; //10
const p3 = +x11*3; //11
const p3 = -x12*4; //-25