39 lines
1.1 KiB
JavaScript
39 lines
1.1 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 op = form.selected.options[index].textContent;
|
||
|
let res = "";
|
||
|
|
||
|
switch(op) {
|
||
|
case "+":
|
||
|
res = await fetch(`http://localhost:8080/sum?num1=${form.num1.value}&num2=${form.num2.value}`)
|
||
|
res = await res.text();
|
||
|
break;
|
||
|
case "-":
|
||
|
res = await fetch(`http://localhost:8080/diff?num1=${form.num1.value}&num2=${form.num2.value}`)
|
||
|
res = await res.text();
|
||
|
break;
|
||
|
|
||
|
case "*":
|
||
|
res = await fetch(`http://localhost:8080/multiply?num1=${form.num1.value}&num2=${form.num2.value}`)
|
||
|
res = await res.text();
|
||
|
break;
|
||
|
|
||
|
case "/":
|
||
|
if(form.num2.value == 0) return;
|
||
|
res = await fetch(`http://localhost:8080/divide?num1=${form.num1.value}&num2=${form.num2.value}`)
|
||
|
res = await res.text();
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
info.textContent = res;
|
||
|
}
|