This repository has been archived on 2020-10-02. You can view files and clone it, but cannot push or open issues or pull requests.
personal-home-calc/script.js
2020-10-02 12:56:17 +03:00

89 lines
2.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var n1='0';
var tp='';
var n2='0';
var sol='0';
var out='0';
var allow=true;
function calc() {
out+='=';
document.getElementById('prompt').innerHTML=out;
var number1=n1;
var oper=tp;
var number2=n2;
$.ajax({
url: "calc.php",
type: "GET",
data: {'number1': number1, 'oper': oper, 'number2': number2},
dataType: "json",
success: function(response) {
if (response.rcode=="0") {
alert(response.result)
clr()
} else {
sol=response.result.toString()
if ((out+sol).length>14) {
if (sol.includes('.')) {sol=sol.substr(0,Math.max(14-out.length,sol.indexOf('.')+2));
} else {
alert('Too large number to display!');
clr()
return;
}
}
out+=sol;
document.getElementById('prompt').innerHTML=out;
allow=false;
n1='0';
tp='';
n2='0';
}
}
});
}
function numb(num) {
if (num=='.') {
if (out[out.length-1]=='.') {return}
if (tp=='') {n1+='.';} else {n2+='.';}
out+='.';
document.getElementById('prompt').innerHTML=out;
} else if (n1=='0' && (tp=='')) {
if (!allow) {clr();}
n1=num;
out=String(n1);
document.getElementById('prompt').innerHTML=out;
} else if ((n1!='0') && (tp=='')) {
n1+=num;
out=String(n1);
document.getElementById('prompt').innerHTML=out;
} else if (n2=='0' && tp!='') {
n2=num;
out=String(n1)+tp+String(n2);
document.getElementById('prompt').innerHTML=out;
} else if (n2!='0' && tp!='') {
n2+=num;
out=String(n1)+tp+String(n2);
document.getElementById('prompt').innerHTML=out;
}
}
function oper(op) {
if (tp!='') {alert('За раз можно использовать только одну операцию!');return;}
if (!allow) {
n1=Number(sol);
out=sol;
allow=true;
}
// if (n1=='0' && n2=='0' && tp=='' && Number(document.getElementById('prompt').value)>0) {
// n1=document.getElementById('prompt').innerHTML
// }
tp=op;
out+=tp;
document.getElementById('prompt').innerHTML=out;
}
function clr() {
n1='0';
tp='';
n2='0';
sol='0';
out='0';
allow=true;
numb(0);
}