forked from nhattruongniit/learn-javascripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigh-order-function.js
More file actions
39 lines (33 loc) · 907 Bytes
/
high-order-function.js
File metadata and controls
39 lines (33 loc) · 907 Bytes
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
var triple = function(x) {
return x * 3;
}
var waffle = triple;
var result = waffle(30);
console.log(result);
var animals = [
{ name: 'Fluffykins', species: 'rabbit' },
{ name: 'Caro', species: 'dog' },
{ name: 'Hamilton', species: 'dog' },
{ name: 'Harold', species: 'fish' },
{ name: 'Ursula', species: 'cat' },
{ name: 'Jimmy', species: 'fish' },
]
/* normal function */
var dogs = [];
for(var i = 0; i < animals.length; i++) {
if(animals[i].species == 'dog') {
dogs.push(animals[i])
}
}
console.log('normal function: ', dogs);
/* avdance function */
var isDog = function(animal) {
return animal.species === 'dog';
};
var isCat = function(animal) {
return animal.species === 'cat';
};
var dogsAdvance = animals.filter(isDog);
var catAdvance = animals.filter(isCat);
console.log('advance function dogs: ', dogsAdvance);
console.log('advance function cat: ', catAdvance);