Created
August 3, 2020 03:35
-
-
Save jimmitt/66b257ef31db86e078153a4e2774c035 to your computer and use it in GitHub Desktop.
Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Calculator</title> | |
</head> | |
<body> | |
<table width="100px" style="text-align: center; vertical-align: middle;" border="2px"> | |
<tr> | |
<td colSpan="4"><input type="text" id="display" style="width: 110px;" disabled/></td> | |
</tr> | |
<tr> | |
<td><button type="button" id="btn7" onClick="btnDigit(7);">7</button></td> | |
<td><button type="button" id="btn8" onClick="btnDigit(8);">8</button></td> | |
<td><button type="button" id="btn9" onClick="btnDigit(9);">9</button></td> | |
<td><button type="button" id="btnDivide" onClick="btnDivide();">/</button></td> | |
</tr> | |
<tr> | |
<td><button type="button" id="btn4" onClick="btnDigit(4);">4</button></td> | |
<td><button type="button" id="btn5" onClick="btnDigit(5);">5</button></td> | |
<td><button type="button" id="btn6" onClick="btnDigit(6);">6</button></td> | |
<td><button type="button" id="btnMultiply" onClick="btnMultiply();">*</button></td> | |
</tr> | |
<tr> | |
<td><button type="button" id="btn1" onClick="btnDigit(1);">1</button></td> | |
<td><button type="button" id="btn2" onClick="btnDigit(2);">2</button></td> | |
<td><button type="button" id="btn3" onClick="btnDigit(3);">3</button></td> | |
<td><button type="button" id="btnSubtract" onClick="btnSubtract();">-</button></td> | |
</tr> | |
<tr> | |
<td><button type="button" id="btnNegate" onClick="btnNegate();">±</button></td> | |
<td><button type="button" id="btnZero" onClick="btnDigit(0);">0</button></td> | |
<td><button type="button" id="btnDecimal" onClick="btnDecimal();">.</button></td> | |
<td><button type="button" id="btnAdd" onClick="btnAdd();">+</button></td> | |
</tr> | |
<tr> | |
<td colSpan="2"><button type="button" id="btnClear" onClick="btnClear();" style="width: 100%;">C</button></td> | |
<td colSpan="2"><button type="button" id="btnEquals" onClick="btnEquals();" style="width: 100%;">=</button></td> | |
</tr> | |
</table> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var prev = 0; | |
var curr = 0; | |
var sign = 1; | |
var display = document.getElementById("display"); | |
var lastOp; | |
var lastCurr = 0; | |
function btnDigit(x) { | |
display.value = display.value+x.toString(); | |
curr = parseFloat(display.value); | |
} | |
function btnClear() { | |
display.value = ""; | |
curr = 0; | |
prev = 0; | |
} | |
function btnAdd() { | |
prev = prev+curr; | |
lastOp = btnAdd; | |
lastCurr = curr; | |
display.value = ""; | |
curr = 0; | |
} | |
function btnSubtract() { | |
prev = prev-curr; | |
lastOp = btnSubtract; | |
lastCurr = curr; | |
display.value = ""; | |
curr = 0; | |
} | |
function btnEquals() { | |
if (display.value === "") { | |
curr = lastCurr; | |
} | |
lastOp(); | |
display.value = prev.toString(); | |
curr = lastCurr; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment