39 lines
978 B
JavaScript
39 lines
978 B
JavaScript
const calculateButton = document.getElementById("calculate");
|
|
const first = document.getElementById("first");
|
|
const second = document.getElementById("second");
|
|
const select = document.getElementById("operation");
|
|
const result = document.getElementById("result");
|
|
|
|
calculateButton.onclick = function() {
|
|
calculate();
|
|
};
|
|
|
|
function calculate() {
|
|
switch (parseInt(select.value)) {
|
|
case 1:
|
|
doSmth("plus")
|
|
break;
|
|
case 2:
|
|
doSmth("minus")
|
|
break;
|
|
case 3:
|
|
doSmth("mult")
|
|
break;
|
|
case 4:
|
|
doSmth("div")
|
|
break;
|
|
};
|
|
}
|
|
|
|
function checkNum(res) {
|
|
if (res.indexOf(".") != -1)
|
|
return parseInt(res)
|
|
else
|
|
return res
|
|
}
|
|
|
|
function doSmth(address) {
|
|
fetch(`http://localhost:8080/${address}?first=${first.value}&second=${second.value}`)
|
|
.then(response => response.text())
|
|
.then(res => result.innerHTML = checkNum(res));
|
|
} |