Reputation: 2900
HTML & JS
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<!-- Allows me to use jQuery -->
<script>
if (typeof module === 'object') {window.module = module; module = undefined;}
</script>
<script src="assets/libraries/jq.js"></script>
<link rel="stylesheet" href="assets/css/style.css">
<!-- End of "Allows me to use jQuery" -->
<script>
if (window.module) module = window.module;
</script>
</head>
<body>
<!-- "vysledek" means in English "result" -->
<input id="vysledek" type="text" autofocus>
<br>
<!-- "kalkulacka" means in my language "calculator" -->
<button class="C" onClick="kalkulacka('C')">C</button>
<button onClick="kalkulacka('(')">(</button>
<button onClick="kalkulacka(')')">)</button>
<button onClick="kalkulacka('/')">÷</button>
<br>
<button onClick="kalkulacka(7)">7</button>
<button onClick="kalkulacka(8)">8</button>
<button onClick="kalkulacka(9)">9</button>
<button onClick="kalkulacka('*')">×</button>
<br>
<button onClick="kalkulacka(4)">4</button>
<button onClick="kalkulacka(5)">5</button>
<button onClick="kalkulacka(6)">6</button>
<button onClick="kalkulacka('-')">−</button>
<br>
<button onClick="kalkulacka(1)">1</button>
<button onClick="kalkulacka(2)">2</button>
<button onClick="kalkulacka(3)">3</button>
<button onClick="kalkulacka('+')">+</button>
<br>
<button onClick="kalkulacka('.')">,</button>
<button onClick="kalkulacka(0)">0</button>
<button onClick="kalkulacka('=')">=</button>
<button onClick="kalkulacka('⌫')">⌫</button>
</body>
</html>
<script>
// "boxik" here just represents that input field
const boxik = document.getElementById("vysledek");
function kalkulacka(x) {
if (x == "C") {
//This clears whole input
boxik.value = "";
} else if (x == "⌫") {
//This removes last digit
boxik.value = boxik.value.substring(0, boxik.value.length-1);
} else if (x == "=") {
//Here is our problem, I expect this to check if it was calculated or
//not
if (eval(boxik.value)) {
boxik.value = eval(boxik.value);
} else {
$("#vysledek").css("border-bottom", "2px solid #bd1515");
}
}
//This basically adds all digits, + etc. together
else {
boxik.value += x;
}
}
</script>
Here is working JSFiddle: https://jsfiddle.net/mg7zmLos/7/
So basically today I started with Electron and I'm working on calculator app which I just hard-coded, but it works (it isn't completed so far) but I want to check if my calculation went good or not, if yes then I just want to do boxik.value = eval(boxik.value)
and if it screws then make my border red using jQuery $("#vysledek").css("border-bottom", "2px solid #bd1515")
but I don't know how to check if it screwed or not...
Upvotes: 0
Views: 704
Reputation: 13672
Get rid of the if, instead put a try/catch around the eval
like this:
try {
boxik.value = eval(boxik.value);
$("#vysledek").css("border-bottom", "2px solid #5faed0");
}
catch (e) {
$("#vysledek").css("border-bottom", "2px solid #bd1515");
}
Upvotes: 1