-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_reduceFunction.js
More file actions
62 lines (44 loc) · 1.39 KB
/
19_reduceFunction.js
File metadata and controls
62 lines (44 loc) · 1.39 KB
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
// unlike its name "reduce" function does not reduce anything
// "reduce" function is to take out a single value out of array of elements
const arr = [5, 3, 2, 6, 7];
// writing in the non functional way first
// sum of elements of the array
function findSum(x){
let sum = 0;
for(let i = 0 ; i < arr.length ; i++){
sum = sum + x[i];
}
return sum;
}
console.log(findSum(arr));
// syntax of reduce function
// array_name.reduce(function_name, initial_value_of_acc)
// function name will contain:
// function(acc , curr){.....}
// here acc plays the same role as sum, where as curr represents the current value(i.e element) in the array
const output = arr.reduce(function(acc, curr){
acc = acc + curr;
return acc;
}, 0)
console.log("sum using reduce function is :",output)
// ----------------------
// now trying to find out the max value among all the element in the array
// without using functional javascript
function maxCheck(x){
let max = 0;
for( i = 0 ; i< x.length ; i++){
if(x[i] > max){
max = x[i];
}
}
return max;
}
console.log("the max among all the element is : ", maxCheck(arr));
// now by using functional javascript
const outputMax = arr.reduce(function(max, curr){
if( curr > max){
max = curr;
}
return max;
}, 0);
console.log("the max value using reduce function is :" , outputMax);