-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-journal.js
235 lines (184 loc) · 4.93 KB
/
code-journal.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
/* Variables - containers that store values */
var name // a declared variable, but not initialized and it's in the global scope (BAD)
let foo // a declared variable that can be changed
const bar // a declared variable that cannot be changed - short for 'constant'
const ANSWER = 42 // const is declared and initialized with the value 42
// String
let string1 = 'Hello World!' // read '=' as 'is assigned the value of...'
let string2 = "Hello Utah!"
let string3 = new String('Hello World!')
// Number
let myNum = 2083972347
let myNum2 = 75.43
'1' == 1 // this statement is true because of type coercion and loose equality checking
'1' === 1 // false because this is strict equality checking
// Boolean
let myBool = true
// Array
let myArray = [] // this is an empty array
// 0 1 2 3 4
let myArray2 = [42, 'Bob', myBool, ANSWER, true]
let myArray3 = [7, 'jay', false, 13, 'blue']
let secondElement = myArray2[1]
myArray2.push('Thor') // added an element to the end of myArray2
myArray2.unshift('Hello World!') // added an element to the beginning of myArray2
let combinedArray = myArray2.concat(myArray3); // combined both arrays to an new array
let mylongString = '32408usfjalieriweur938u425ksdjfowiur84uwrlwshdjfo8wuroiwejr4e' // just an array of characters
mylongString.length //find the length of the string
// Object
let minObject = {}
const myCar = {
make: "Chevrolet",
color: "Red",
year: "1965",
vin: "2390487sijweoru38lirehs"
};
myCar.numDoors = 2;
const anotherObject = {
wordz: ["foo", "bar", "baz"],
car: {
make: "McLaren",
model: "675LT"
},
awesomeness: true
};
// Functions
function myFunction() {
return "My greeting to you...";
}
function sumTwoThings(one, two) {
// watch out for data type issues here!
return one + two; // if numbers, will add them. If strings, will concatenate.
}
// Arrow Functions
// a higher order function is a function that accepts another function as a parameter.
// filter, map and reduce are the most popular, but forEach, every, find, and some are also HOFs
const theFunction = () => { //multiple lines use curly braces and 'return' keyword
return 'I am awesome'
}
// Filter method example. Filter returns an array of all elements that 'pass the test'
const pilots = [
{
id: 2,
name: "Wedge Antilles",
faction: "Rebels"
},
{
id: 8,
name: "Ciena Ree",
faction: "Empire"
},
{
id: 40,
name: "Iden Versio",
faction: "Empire"
},
{
id: 66,
name: "Thane Kyrell",
faction: "Rebels"
}
];
const rebels = pilots.filter((pilot) => pilot.faction === "Rebels");
const empire = pilots.filter((pilot) => {
return pilot.faction === "Empire";
});
// Array helper method 'map'
let filmURLs = [
"https://swapi.co/api/films/",
"https://swapi.co/api/films/5/",
"https://swapi.co/api/films/4/this one is longer... even longer",
"https://swapi.co/api/films/6/",
"https: ",
"https://swapi.co/api/films/1/"
];
const filmLengths = filmURLs.map((filmURL) => filmURL.length);
const filmPlusMore = filmURLs.map((filmURL) => {
let filmObj = {
index: filmURL,
foo: 'Bar'
}
return filmObj
})
// Reduce example
const swpilots = [
{
id: 10,
name: "Poe Dameron",
years: 14
},
{
id: 2,
name: "Temmin 'Snap' Wexley",
years: 30
},
{
id: 41,
name: "Tallissan Lintra",
years: 16
},
{
id: 99,
name: "Ello Asty",
years: 22
}
];
const totalYears = swpilots.reduce((acc, pilot) => acc + pilot.years, 0);
const mostExpPilot = swpilots.reduce((oldest, pilot) => {
return (oldest.years || 0) > pilot.years ? oldest : pilot;
}, {});
// Now we combine map, reduce, and filter
var personnel = [
{
id: 5,
name: "Luke Skywalker",
pilotingScore: 98,
shootingScore: 56,
isForceUser: true
},
{
id: 82,
name: "Sabine Wren",
pilotingScore: 73,
shootingScore: 99,
isForceUser: false
},
{
id: 22,
name: "Zeb Orellios",
pilotingScore: 20,
shootingScore: 59,
isForceUser: false
},
{
id: 15,
name: "Ezra Bridger",
pilotingScore: 43,
shootingScore: 67,
isForceUser: true
},
{
id: 11,
name: "Caleb Dume",
pilotingScore: 71,
shootingScore: 85,
isForceUser: true
}
];
let jediPersonnel = personnel.filter((person) => person.isForceUser);
let jediScores = jediPersonnel.map(
(jedi) => jedi.pilotingScore + jedi.shootingScore
);
let totalJediScore = jediScores.reduce((acc, score) => acc + score, 0);
const totalJediScoreChained = personnel
.filter((person) => person.isForceUser)
.map((jedi) => jedi.pilotingScore + jedi.shootingScore)
.reduce((acc, score) => acc + score, 0);
// Ternary operator syntax: condition ? exprIfTrue : exprIfFalse
const totalJediScoreReduce = personnel.reduce(
(acc, person) =>
person.isForceUser
? acc + person.pilotingScore + person.shootingScore
: acc,
0
);