-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.js
331 lines (264 loc) · 6.32 KB
/
practice.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
const starwars = [
{ name: 'Luke', age: 28, gender: 'male'},
{ name: 'Solo', age: 26, gender: 'male'},
{ name: 'Lia', age: 28, gender: 'female'}
]
// map
const names = starwars.map((character) => character.name)
console.log(names)
const age = starwars.map((character) => character.age)
console.log(age)
const isBoys = starwars.filter((character) => character.gender === 'male')
console.log(isBoys)
const ageOver28 = starwars.map((character) => character.age > 26)
console.log(ageOver28)
const youngOrOld = starwars.map((character) => (character.age > 26)? 'Old' : 'Young')
console.log(youngOrOld)
// for loop
// const female = []
// for (var i = 0; i < starwars.length; i ++) {
// if (starwars[i].gender === 'female')
// female.push(starwars[i])
// }
// console.log(female)
//filter
const female = starwars.filter((female) => female.gender === 'female')
console.log(female)
// reduce Ex1
const orders = [
{ amount: 450 },
{ amount: 500 },
{ amount: 250 },
{ amount: 300 },
{ amount: 125 },
]
const totalAmount = orders
.reduce((sum, order) => sum + order.amount, 0)
console.log(totalAmount)
//reduce Ex2
const someOrder = {
items: [
{ name: 'Dog food', price: 10, quantity: 5},
{ name: 'Dog cage', price: 500, quantity: 3 }
]
}
const orderTotal = order => order.items
.reduce((prev, cur) => prev + (cur.price * cur.quantity), 0)
result = orderTotal(someOrder)
result
//Recursion ->when a function call itself, until it doesn't
let countDownForm = (num) => {
if (num === 0) return
console.log(num) //mini的數字到0
countDownForm(num - 1)
}
countDownForm(10)
// Iterative FNub
const FNum = (n) => {
if( n === 1 ) {
return 1
} else {
return n * FNum( n-1 )
}
}
console.log(FNum(3))
// n! = n * (n-1) * n(-2) * .... * 1
// iter (3,1)
// iter (2,3)
// iter (1,6)
// Answer : 6
const IterNum = (num) => {
const iter = (counter, acc) => { //acc -> accumulator累加器
if ( counter === 1 ) {
return acc
}
return iter( counter-1, counter*acc )
}
return iter(num, 1)
}
console.log(IterNum(3))
let categories = [
{ id: 'animals', 'parent': null },
{ id: 'mammals', 'parent': 'animals' },
{ id: 'dogs', 'parent': 'mammals' },
{ id: 'cats', 'parent': 'mammals' },
{ id: 'chihuahua', 'parent': 'dogs'},
{ id: 'persian', 'parent': 'cats'}
]
let makeTree = (categories, parent) => {
let node = {}
//use filter catch id:animals
categories
.filter(c => c.parent === parent)
.forEach(c =>
node[c.id] = makeTree(categories, c.id))
return node
}
console.log(
JSON.stringify(
makeTree(categories, null)
, null, 2)
)
/*
{
animals: {
mammals: {
dogs: {
chihuahua: null
},
cats: {
persian: null
}
}
}
}*/
const myObj = {
ary : [1,3,5],
firstAdd : (a, b) => a + b,
secondAdd : (c) => myObj.ary[0] + c,
}
console.log(myObj.ary[0])
console.log(myObj.ary[1])
console.log(myObj.ary[2])
myObj.ary.forEach((element) => {console.log(element)})
console.log(myObj.firstAdd(1,5))
console.log(myObj.secondAdd(9))
//8/17 Example1 bind
let talk = () => {
console.log(boromir.sound)
}
let boromir = {
blabber: talk,
sound: 'One does not simpoly walk into Modor !'
}
boromir.speak = talk.bind(boromir)
let blabber = boromir.speak
blabber()
// Example2 bind
let talkMe = () => {
console.log(me.sound)
}
let me = {
sound: 'HAHAHA',
speak: talkMe
}
me.speak()
// this Example
const objA = {
test () {
console.log(this)
console.log(this === objA)
console.log(objA)
}
}
objA.test()
// Prototype Example
function talking () {
console.log(this.sound)
}
let animal = {
talking
}
let dog = {
sound: 'WOOOF'
}
let GGDog = {
barbar: function () {
console.log(this.sound.toUpperCase())
}
}
// 連結到Prototype
Object.setPrototypeOf(dog, animal)
animal.talking = function () {
console.log('Hey I"m Here')
}
dog.talking()
Object.setPrototypeOf(GGDog, dog)
GGDog.barbar()
// 8/18 'new'
// function Person (saying) {
// this.saying = saying
// }
// Person.prototype.talk = function () {
// console.log('我說:', this.saying)
// }
// var Luke = new Person ('我好帥')
// Luke.talk()
// rebuild new function
function Person (saying) {
this.saying = saying
}
Person.prototype.talk = function () {
console.log('我說:', this.saying)
}
// 1. rebuild a new function
function NEW (constructor) {
var obj = {}
Object.setPrototypeOf(obj, constructor.prototype) // 2.set the prototype
var argsAraay = Array.prototype.slice.apply(arguments) // 把 arguments 轉成 array, 才能許用slice
constructor.apply(obj, argsAraay.slice(1)) // 3.execute constructor with 'this'
return obj // 4. return created object
}
var Luke = NEW (Person, '我好帥')
Luke.talk()
// 8/26 JSON clone
let firstObj = {a:1, b:2}
let secondObj = JSON.parse(JSON.stringify(firstObj))
let thirdObj = {c: 5}
console.log(firstObj, secondObj)
secondObj.b = 3
console.log(firstObj, secondObj)
// 8/26 JSON merge 合併
Object.assign(firstObj,thirdObj)
console.log(firstObj)
// Javascript sort
const arys= [52,125,4,7,235,1221,8,1,12,32,89]
const sortAsc = arys.sort((a,b) => a-b)
console.log(sortAsc)
const sortDesc = arys.sort((a,b) => b-a)
console.log(sortDesc)
// 8/29 splice
let arr = ['a', 'b', 'c', 'd', 'e']
arr.splice(1, 2, 'haha')
console.log(arr)
// 9/4 TwoSum function
let twoSum = (numArray, sum) => {
let pairs = []
let hashtable = []
for (let i = numArray.length; i--;) { // let i = 0 ,len = numArray.length;i<len;i++
let currNum = numArray[i]
let counterpart = sum - currNum
if (hashtable.indexOf(counterpart) !== -1) { //-1 是找不到字串的number
pairs.push([currNum, counterpart])
}
hashtable.push(currNum)
}
return pairs
}
console.log(twoSum([1, 6, 4, 5, 3, 3], 7))
// Closure
const myName = 'Luke Lan'
let greetMe = () => {
console.log('Hello, ' + myName + '!' )
}
greetMe()
// currying
let aPersonName =
name =>
gender =>
nickname =>
name + ', 你好\n' +
'您的性別是:' + gender + '\n' +
'嗨,' + nickname + '!'
let output = aPersonName('藍藍路')('男')('小藍')
console.log(output)
// Fizz Buzz
let fizzBuzz = (num) => {
for(let i = 1; i<num ;i++ ) {
if (i % 15 === 0) console.log('C')
else if (i % 3 === 0) console.log('a')
else if (i % 5 ===0) console.log('b')
else console.log(i)
}
}
fizzBuzz(5)