-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (27 loc) · 952 Bytes
/
index.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
// Import stylesheets
import './style.css';
const $formAddNum = document.querySelector('#formAddNum');
$formAddNum.addEventListener('submit', (event) => {
event.preventDefault();
const $firstNum = parseFloat(document.querySelector('#firstNum').value);
const $secondNum = parseFloat(document.querySelector('#secondNum').value);
const $operator = document.querySelector(
'input[name=operator]:checked'
).value;
const calculation = {
'+': function ($firstNum, $secondNum) {
return $firstNum + $secondNum;
},
'-': function ($firstNum, $secondNum) {
return $firstNum - $secondNum;
},
'/': function ($firstNum, $secondNum) {
return $firstNum / $secondNum;
},
};
function round(a, b) {
return Number(Math.round(a + 'e' + b) + 'e-' + b).toFixed(b);
}
const result = round(calculation[$operator]($firstNum, $secondNum), 2);
document.querySelector('#resultAdd').value = result;
});