-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.js
187 lines (149 loc) · 4.07 KB
/
examples.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
/**
* Variables
*/
var x = 1; // Old style, function scope
let y = 2; // New style, block scope
const z = 3;
console.log('After let y = 2, y is: ' + y);
y = 5;
console.log('After y = 5, y is: ' + y); // What is y?
console.log('After const z = 3, z is: ' + z);
//z = 6;
console.log('After z = 6, z is: ' + z); // z is still 3
/**
* Control structures
*/
if ( 10 == "10") { // actually true and should be avoided unless desired
console.log("This statement is true");
} else {
console.log("This statement is false");
}
if ( 10 === "10") { //false as one would expect
console.log("This statement is true");
} else {
console.log("This statement is false");
}
// switch statements
let value = 2;
switch (value) {
case 1:
console.log("value is 1");
break;
case 2:
console.log("value is 2");
break;
case 3:
console.log("value is 3");
break;
case 4:
console.log("value is 4");
break;
case 5:
console.log("value is 5");
break;
case 6:
console.log("value is 6");
break;
default:
console.log("The value is not an integer in the range of 1 through 6");
}
//advanced control(ternary operator)
let someValue = (2 > 1 ? "the condition is true" : "the condition is false");
console.log(someValue);
/**
* Loops
*/
//for loops
for(let i = 0; i < 10; i++){//best used when the amount of iterations is known easily
console.log(i);
}
let values = [10, 3, 10, 34, 23, 1, 5434];
for (value in values) {
console.log();
}
//while loops
let num = 1;
while(num < 10){//best used when the amount of iterations is not known easily
console.log(num);
num++;
}
/**
* Functions
*/
function myFunction() {
console.log("I am a function!");
}
myFunction();
let myFunction2 = () => {
console.log("I am also a function!");
}
myFunction2();
let myFunction3 = (arg1,arg2) => {
return arg1 + arg2;
}
console.log(myFunction3(10, 3));
// JavaScript is perfect for functional programming
const addTwo = (n) => { return n + 2; }
const list = [1, 2, 3];
const map = (func, arr) => {
newArr = [];
arr.forEach((num) => {
newArr.push(func(num));
});
return newArr;
}
// It turns out that map is already built in to JavaScript
const numbers = [5, 10, 15];
console.log(numbers.map(x => x * 2)); // Multiply each element in array by two
console.log(numbers); // Original array is not changed
/**
* Modifying the DOM
*/
let element = document.getElementById("subtitle");
element.innerHTML = "This is a new subtitle!!";
/**
* Sending and receiving data
*/
// free and open apis --> https://any-api.com/
// does not work if accessed locally as in file:// or c://
function getData(action, callback) {
let xhr = new XMLHttpRequest();
xhr.onload = () => {//success
callback(null,xhr.responseText);
}
xhr.onerror = () => {//failed
callback("Data Request failed: " + action);
}
xhr.open("get", action, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send();
}
getData("https://swapi.co/api/people/1/", (err, data) => {
if(err) throw err;//check for errors first
let element = document.getElementById("content");
element.innerHTML = "XHR Response: " + data;
console.log(data);
});
/**
* Exports and imports
*/
import Dog from './module.js';
import { printAlphabet } from './module.js';
const puppy = new Dog(20, 50);
console.log(puppy.bark());
console.log(puppy.getDoggyInfo());
console.log(printAlphabet());
/*
Types of import statements:
import defaultExport from "module-name";
import * as name from "module-name";
import { export } from "module-name";
import { export as alias } from "module-name";
import { export1 , export2 } from "module-name";
import { foo , bar } from "module-name/path/to/specific/un-exported/file";
import { export1 , export2 as alias2 , [...] } from "module-name";
import defaultExport, { export [ , [...] ] } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";
var promise = import(module-name);
*/