So, I wanted to make it so that if, for some reason, people mistyped/misclicked something on my calculator, it would show a syntax error. However, I do not know how to do so.
What I mean by “mistyped” is when someone might have accidentally written something like:
10 + / 10
when they actually wanted to write 10 + 10.
Currently, my calculator just shows the equation inputted when there is an error, and doesn’t do anything else. I just want to add the function for efficiency’s sake.
<html>
<head>
<script>
function dis(val)
{
document.getElementById("result").value+=val
}
function solve()
{
let x = document.getElementById("result").value
let y = eval(x)
document.getElementById("result").value = y
}
function clr()
{
document.getElementById("result").value = " "
}
</script>
<style>
.title{
margin-bottom: 10px;
text-align:center;
width: 100%;
color:Black;
border: solid gray 2px;
font-size:30px
}
input[type="button"]
{
background-color:gray;
color: black;
border: solid black 2px;
width:100%;
font-size:30px
}
input[type="button"]:hover{
background-color:#D3D3D3;
cursor:pointer;
}
input[type="text"]
{
background-color:white;
border: solid black 2px;
width:100%;
font-size:30px
}
</style>
</head>
<body>
<div class = title >Calculator</div>
<table border="10" style="width:100%;">
<tr>
<td colspan="3"><input type="text" id="result"/></td>
</tr>
<tr>
<td><input type="button" value="1" onclick="dis('1')"/> </td>
<td><input type="button" value="2" onclick="dis('2')"/> </td>
<td><input type="button" value="3" onclick="dis('3')"/> </td>
<td><input type="button" value="/" onclick="dis(' / ')"/> </td>
<td><input type="button" value="c" onclick="clr()"/> </td>
</tr>
<tr>
<td><input type="button" value="4" onclick="dis('4')"/> </td>
<td><input type="button" value="5" onclick="dis('5')"/> </td>
<td><input type="button" value="6" onclick="dis('6')"/> </td>
<td><input type="button" value="(" onclick="dis('(')"/> </td>
<td><input type="button" value=")" onclick="dis(')')"/> </td>
</td>
</tr>
<tr>
<td><input type="button" value="7" onclick="dis('7')"/> </td>
<td><input type="button" value="8" onclick="dis('8')"/> </td>
<td><input type="button" value="9" onclick="dis('9')"/> </td>
<td><input type="button" value="+" onclick="dis(' + ')"/> </td>
<td><input type="button" value="-" onclick="dis(' - ')"/>
</tr>
<tr>
<td><input type="button" value="." onclick="dis('.')"/> </td>
<td><input type="button" value="0" onclick="dis('0')"/> </td>
<td><input type="button" value="=" onclick="solve()"/> </td>
<td><input type="button" value="×" onclick="dis(' * ')"/> </td>
<td><input type="button" value="π" onclick="dis('3.1415926535')"></td>
</tr>
</table>
</body>
</html>`