File tree Expand file tree Collapse file tree 1 file changed +18
-6
lines changed
JavaScript3/Week7/classwork/reduce Expand file tree Collapse file tree 1 file changed +18
-6
lines changed Original file line number Diff line number Diff line change @@ -7,25 +7,37 @@ people = [
7
7
{ name : 'Badr' , age : 99 }
8
8
]
9
9
10
-
10
+ // Filter example
11
11
const underagePeople = people . filter ( function ( person ) {
12
12
return person . age < 18
13
13
} )
14
14
15
- console . log ( underagePeople )
15
+ console . log ( 'Under age people: ' , underagePeople )
16
16
17
+ // Map example
17
18
const ages = people . map ( function ( person ) {
18
19
return person . age
19
20
} )
20
21
21
- console . log ( ages )
22
+ console . log ( 'All ages: ' , ages )
23
+
24
+
25
+
26
+ // Adding all ages with a for loop
27
+ let totalAge = 0 ;
28
+ for ( let i = 0 ; i < people . length ; i ++ ) {
29
+ totalAge = totalAge + people [ i ] . age ;
30
+ }
31
+
32
+ console . log ( 'Total age for loop: ' , totalAge )
22
33
23
34
24
- function reduceTotalAge ( accumulator , currentValue ) {
35
+ // Adding all ages with reduce
36
+ function reduceFunction ( accumulator , currentValue ) {
25
37
console . log ( accumulator , currentValue )
26
38
return accumulator + currentValue . age
27
39
}
28
40
29
- const totalAge = people . reduce ( reduceTotalAge , 0 )
41
+ const totalAgeReduce = people . reduce ( reduceFunction , 0 )
30
42
31
- console . log ( 'total age: ' + totalAge )
43
+ console . log ( 'Total age reduce : ' , totalAgeReduce )
You can’t perform that action at this time.
0 commit comments