JavaScript Calculator

A calculator is a device that performs arithmetic operations on numbers. The simplest calculators can do only addition, subtraction, multiplication, and division.

<html>
<body>
<table border="1">
<tr>
<td colspan="4"><input id="t1" style="text-align: right;" type="text" /></td>
</tr>
<tr>
<td><input onclick="dis('1')" type="button" value="1" /> </td>
<td><input onclick="dis('2')" type="button" value="2" /> </td>
<td><input onclick="dis('3')" type="button" value="3" /> </td>
<td><input onclick="dis('4')" type="button" value="4" /> </td>
</tr>
<tr>
<td><input onclick="dis('5')" type="button" value="5" /> </td>
<td><input onclick="dis('6')" type="button" value="6" /> </td>
<td><input onclick="dis('7')" type="button" value="7" /> </td>
<td><input onclick="dis('8')" type="button" value="8" /> </td>
</tr>
<tr>
<td><input onclick="dis('9')" type="button" value="9" /> </td>
<td><input onclick="dis('0')" type="button" value="0" /> </td>
<td><input onclick="dis('+')" type="button" value="+" /> </td>
<td><input onclick="dis('-')" type="button" value="-" /> </td>
</tr>
<tr>
<td><input onclick="dis('*')" type="button" value="*" /> </td>
<td><input onclick="dis('/')" type="button" value="/" /> </td>
<td><input onclick="dis('.')" type="button" value="." /> </td>
<td><input onclick="solve()" type="button" value="=" /> </td>
</tr>
<tr align="center"> 
<td colspan="4"><input onclick="clr()" type="button" value="clear" /> </td>   
</tr>
</table>
</body>
    <script>
    function dis(val)
{
document.getElementById("t1").value+=val
 }
function solve()
{
let x = document.getElementById("t1").value
let y = eval(x)
document.getElementById("t1").value = y
}
function clr()
{
document.getElementById("t1").value = ""
}
</script>
</html>

output



Post a Comment

Previous Post Next Post