-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculations.js
24 lines (19 loc) · 949 Bytes
/
calculations.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
function calculate(calculationHtmlId, calculationFunc) {
var calculationHtmlElement = document.getElementById(calculationHtmlId);
if (!calculationHtmlElement) {
throw new Error('Could not find HTML element with ID "' + calculationHtmlId + '"');
}
var aElement = calculationHtmlElement.getElementsByClassName('a')[0];
var bElement = calculationHtmlElement.getElementsByClassName('b')[0];
var resultElement = calculationHtmlElement.getElementsByClassName('result')[0];
function performCalculation() {
var valueOfA = parseFloat(aElement.value);
var valueOfB = parseFloat(bElement.value);
resultElement.textContent = calculationFunc(valueOfA, valueOfB);
}
aElement.addEventListener('change', performCalculation);
aElement.addEventListener('keyup', performCalculation);
bElement.addEventListener('change', performCalculation);
bElement.addEventListener('keyup', performCalculation);
performCalculation();
}