IP_Aleikin_PIbd-22/frontend/js/script.js

59 lines
1.2 KiB
JavaScript

let form = document.getElementById("form");
let info = document.getElementById("res");
form.onsubmit = async (e) => {
e.preventDefault();
if(form.num1.value === "") return;
if(form.num2.value === "") return;
let index = form.selected.selectedIndex;
let indexType = form.type.selectedIndex;
let op = form.selected.options[index].textContent;
let type = form.type.options[indexType].textContent;
let res = "";
function calc() {
switch(op) {
case "+":
return "sum";
break;
case "-":
return "diff";
break;
case "*":
return "mul";
break;
case "/":
return "div";
break;
}
}
function typeOfCalc() {
switch(type) {
case "Int":
return "int";
break;
case "String":
return "string";
break;
case "FloatArray":
return "floatArray";
break;
}
}
if (form.num2.value == 0 && calc(op) == "divide") return;
res = await fetch(`http://localhost:8080/${calc()}?num1=${form.num1.value}&num2=${form.num2.value}&operationType=${typeOfCalc()}`)
res = await res.text();
info.textContent = res;
}