IP_PIbd-21_Balberova_D.N./front/script.js
2023-02-27 17:59:04 +04:00

44 lines
1.2 KiB
JavaScript

const calculateButton = document.getElementById("calculate");
const calculateStrButton = document.getElementById("calculateStr");
const first = document.getElementById("first");
const second = document.getElementById("second");
const select = document.getElementById("operation");
const result = document.getElementById("result");
calculateButton.onclick = function() {
calculate("num");
};
calculateStrButton.onclick = function() {
calculate("str");
};
function calculate(parametr) {
switch (parseInt(select.value)) {
case 1:
doSmth("plus", parametr)
break;
case 2:
doSmth("minus", parametr)
break;
case 3:
doSmth("mult", parametr)
break;
case 4:
doSmth("div", parametr)
break;
};
}
function checkNum(res) {
if (res.indexOf(".") != -1)
return parseInt(res)
else
return res
}
function doSmth(address, type) {
console.log("Тип " + type)
fetch(`http://localhost:8080/${address}?first=${first.value}&second=${second.value}&type=${type}`)
.then(response => response.text())
.then(res => result.innerHTML = checkNum(res));
}