Last active
August 29, 2015 13:56
-
-
Save alexispurslane/8892722 to your computer and use it in GitHub Desktop.
A Calculator that i created to see if I could pass the interview questions at Yahoo!.
This file contains hidden or 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
| button { width: 30px; } | |
| #zero { margin-left: 33px; } | |
| span { | |
| display: block; | |
| width: 92px; | |
| border: 1px solid black; | |
| height: 15px; | |
| margin-bottom: 3px; | |
| padding: 2px; | |
| color: green; | |
| background-color: black; | |
| font-family: courier; | |
| font-weight: bold; | |
| } | |
| #wrapper { | |
| border: 1px solid black; | |
| width: 98px; | |
| padding: 2px; | |
| background-color: lightgrey; | |
| } |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <div id='wrapper'> | |
| <span id='result'></span> | |
| <div id='buttons'> | |
| <div> | |
| <button>7</button> | |
| <button>8</button> | |
| <button>9</button> | |
| </div> | |
| <div> | |
| <button>6</button> | |
| <button>5</button> | |
| <button>4</button> | |
| </div> | |
| <div> | |
| <button>3</button> | |
| <button>2</button> | |
| <button>1</button> | |
| </div> | |
| <div> | |
| <button id='zero'>0</button> | |
| </div> | |
| <div> | |
| <button>+</button> | |
| <button>-</button> | |
| <button>c</button> | |
| </div> | |
| <div> | |
| <button>x</button> | |
| <button>/</button> | |
| <button>=</button> | |
| </div> | |
| </div> | |
| </div> | |
| </body> | |
| </html> |
This file contains hidden or 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 buttons = document.getElementById('buttons'), | |
| result = document.getElementById('result'), | |
| op, a, b, | |
| isDone = false; | |
| buttons.onclick = function (e) { | |
| var which = e.target.innerText, | |
| ops = { | |
| '+': true, | |
| '-': true, | |
| 'x': true, | |
| '/': true, | |
| '=': true | |
| }, | |
| isEquals = which === '='; | |
| if (which === 'c') { | |
| a = b = op = ''; | |
| result.innerText = ''; | |
| } else { | |
| if (ops[which]) { | |
| if (!isEquals) { | |
| a = result.innerText; | |
| result.innerText = ''; | |
| op = which; | |
| } else { | |
| b = result.innerText; | |
| result.innerText = eval(a + op + b); | |
| a = b = op = ''; | |
| isDone = true; | |
| } | |
| } else { | |
| if (isDone) { | |
| result.innerText = which; | |
| isDone = false; | |
| } else { | |
| result.innerText += which; | |
| } | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment