-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.html
More file actions
44 lines (37 loc) · 1.26 KB
/
Copy path10.html
File metadata and controls
44 lines (37 loc) · 1.26 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
<!DOCTYPE html>
<body>
<input id="arr">
<select id="operation">
<option value="sum">Sum</option>
<option value="product">Product</option>
<option value="average">Average</option>
</select>
<button onclick="cal()">Calculate</button>
<div id="b"></div>
<script>
function calculate(arr, callback, operation) {
return callback(arr, operation);
}
function processArray(arr, operation) {
if (operation === "sum") {
return arr.reduce((a, b) => a + b, 0);
} else if (operation === "product") {
return arr.reduce((a, b) => a * b, 1);
} else if (operation === "average") {
return arr.reduce((a, b) => a + b, 0) / arr.length;
} else {
return "Invalid operation!";
}
}
function cal() {
let arr = document.getElementById("arr").value.split(" ").map(Number);
let operation = document.getElementById("operation").value;
let result = calculate(arr, processArray, operation);
document.getElementById("b").innerHTML = `
Operation: ${operation}<br>
Result: ${result}
`;
}
</script>
</body>
</html>