-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay06_Reduce_Find_FindIndex.js
62 lines (33 loc) · 1.8 KB
/
Day06_Reduce_Find_FindIndex.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
//Reduce function
// it accunulates all the values of an array
/// 0+1 =1 +1 =2 +1, 3+ 5
var numbers = [0, 11, 1, 24, 38, 5, 888];
numbers.reduce(
function (accumulator, eachNumber){
console.log('accumulator: '+accumulator);
console.log('each number: '+eachNumber);
return accumulator ;
}
)
//please share me the sum of numbers
var result = numbers.reduce(
(accumulator, eachNumber) => accumulator + eachNumber
)
console.log(result);
//max element of an array using reduce function
console.log(numbers.reduce( (accumulator, max) => accumulator > max? accumulator: max, 0 ));
//Find element
// when we use find function, it looks for a specific condition and returns the first condition
const myNumber = numbers.find(
function (eachItem){
return eachItem > 20;
}
)
console.log(myNumber);
//findIndex function does the same action, but just returns the index of the item
const myNumber2 = numbers.findIndex(
function (eachItem){
return eachItem > 20;
}
)
console.log(myNumber2);