PIbd-22_Karamushko_M_K_IP_Labs/frontend/js/script.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-02-18 20:01:28 +04:00
let form = document.getElementById("form");
let info = document.getElementById("res");
form.onsubmit = async (e) => {
e.preventDefault();
if(form.x.value === "") return;
if(form.y.value === "") return;
let index = form.selected.selectedIndex;
2023-02-27 17:42:27 +04:00
let indexType = form.type.selectedIndex;
2023-02-18 20:01:28 +04:00
let op = form.selected.options[index].textContent;
2023-02-27 17:42:27 +04:00
let type = form.type.options[indexType].textContent;
2023-02-18 20:01:28 +04:00
let res = "";
switch(op) {
case "+":
2023-02-27 17:42:27 +04:00
res = await fetch(`http://localhost:8080/sum${type}?x=${form.x.value}&y=${form.y.value}`)
2023-02-18 20:01:28 +04:00
res = await res.text();
break;
case "-":
2023-02-27 17:42:27 +04:00
res = await fetch(`http://localhost:8080/diff${type}?x=${form.x.value}&y=${form.y.value}`)
2023-02-18 20:01:28 +04:00
res = await res.text();
break;
case "*":
2023-02-27 17:42:27 +04:00
res = await fetch(`http://localhost:8080/multiple${type}?x=${form.x.value}&y=${form.y.value}`)
2023-02-18 20:01:28 +04:00
res = await res.text();
break;
case "/":
if(form.y.value == 0) return;
2023-02-27 17:42:27 +04:00
res = await fetch(`http://localhost:8080/divide${type}?x=${form.x.value}&y=${form.y.value}`)
2023-02-18 20:01:28 +04:00
res = await res.text();
break;
}
info.textContent = res;
}