-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-hypo.js
35 lines (32 loc) · 1.03 KB
/
script-hypo.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
// get required elements
const inputSide = document.getElementsByClassName("input-side");
const btnGetHypo = document.getElementById("btn-get-hypo");
const divOutput = document.getElementById("div-output");
// addEventListener for check btn click
btnGetHypo.addEventListener("click", () => {
// get sum of elements
const sum = getSum();
const hypo = Math.sqrt(sum);
if (sum !== undefined) inform(hypo);
});
// arrow function to calculate sum.
const getSum = () => {
let sum = 0;
for (let i = 0; i < 2; i++) {
if (inputSide[i].value === "" || Number(inputSide[i].value) < 0) {
inform();
return undefined;
} else {
sum += Number(inputSide[i].value) ** 2;
}
}
return sum;
};
// arrow function to set the message (result)
const inform = (hypo = "") => {
let message;
if (hypo !== "") message = `Hypotenuse = <strong>${hypo}</strong>`;
else message = "Enter positive numbers for each sides.";
divOutput.innerHTML = message;
divOutput.className = divOutput.className + " border-norm round-hf";
};