-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
34 lines (30 loc) · 796 Bytes
/
script.js
File metadata and controls
34 lines (30 loc) · 796 Bytes
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
function calculateResults() {
let number = document.getElementById('number').value;
let factorialResult = 1;
let sumResult = 0;
let averageResult = 0;
// Factorial calculation (while loop)
let i = 1;
while (i <= number) {
factorialResult *= i;
i++;
}
// Sum calculation (do-while loop)
let j = 1;
do {
sumResult += j;
j++;
} while (j <= number);
// Average calculation (for loop)
for (let k = 1; k <= number; k++) {
averageResult += k;
}
averageResult /= number;
// Display the results
let results = document.getElementById('results');
results.innerHTML = `
<p><strong>Factorial:</strong> ${factorialResult}</p>
<p><strong>Sum:</strong> ${sumResult}</p>
<p><strong>Average:</strong> ${averageResult.toFixed(2)}</p>
`;
}