-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample4.html
More file actions
56 lines (48 loc) · 1.61 KB
/
Example4.html
File metadata and controls
56 lines (48 loc) · 1.61 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
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example 4</title>
</head>
<body>
<label>Enter value of a:</label>
<input type="number" id="a">
<br>
<label>Enter value of b:</label>
<input type="number" id="b">
<br>
<label>Enter Array values:</label>
<input type="text" id="l">
<br>
<button onclick="calculateSum()">Submit</button>
<script>
function f4(a, b, arr)
{
// a = parseInt(a);
// b = parseInt(b);
// Filter numbers that are multiples of a or b
const multiples =arr.filter(num => num % a === 0 || num % b === 0);
// Sum the multiples
const sumvalue = multiples.reduce((sum, num) => sum + num, 0);
return sumvalue;
}
function calculateSum()
{
const a = document.getElementById("a").value;
const b = document.getElementById("b").value;
const l = document.getElementById("l").value;
// Convert array string to actual array of numbers
const c = l.split(',').map(num => parseInt(num.trim()));
//alert (c);
//validate input values
if(!a||!b||!c)
{
alert("Please fill the fields");
return;
}
const result = f4(a, b, c);
alert("The sum of all multiples of " + a + " or " + b + " is: " + result);
}
</script>
</body>
</html>