Last active
October 14, 2017 17:15
-
-
Save 0xch4z/6bfa78256d5764689f1156af445f749a to your computer and use it in GitHub Desktop.
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 lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>jQuery Calculator</title> | |
| <!-- Added link to the jQuery Library --> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
| <!-- Added a link to Bootstrap--> | |
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> | |
| </head> | |
| <body> | |
| <!-- Jumbotron for Title --> | |
| <div class="jumbotron"> | |
| <h1 class="text-center">jQuery Calculator</h1> | |
| <h3 class="text-center">Perform basic mathematic operations using the power of jQuery!</h3> | |
| </div> | |
| <div class="container"> | |
| <div class="row"> | |
| <!-- Calculator Panel --> | |
| <div class="col-lg-4"> | |
| <div class="panel panel-default"> | |
| <div class="panel-heading"> | |
| <h3 class="panel-title">Calculator</h3> | |
| </div> | |
| <div class="panel-body"> | |
| <button id="button-1" class="btn btn-primary number" value="1"><h1>1</h1></button> | |
| <button id="button-2" class="btn btn-primary number" value="2"><h1>2</h1></button> | |
| <button id="button-3" class="btn btn-primary number" value="3"><h1>3</h1></button> | |
| <button id="button-plus" class="btn btn-danger operator" value="+"><h1>+</h1></button> | |
| <br><br> | |
| <button id="button-4" class="btn btn-primary number" value="4"><h1>4</h1></button> | |
| <button id="button-5" class="btn btn-primary number" value="5"><h1>5</h1></button> | |
| <button id="button-6" class="btn btn-primary number" value="6"><h1>6</h1></button> | |
| <button id="button-minus" class="btn btn-danger operator" value="-"><h1>−</h1></button> | |
| <br><br> | |
| <button id="button-7" class="btn btn-primary number" value="7"><h1>7</h1></button> | |
| <button id="button-8" class="btn btn-primary number" value="8"><h1>8</h1></button> | |
| <button id="button-9" class="btn btn-primary number" value="9"><h1>9</h1></button> | |
| <button id="button-multiply" class="btn btn-danger operator" value="*"><h1>×</h1></button> | |
| <br><br> | |
| <button id="button-0" class="btn btn-primary number" value="0"><h1>0</h1></button> | |
| <button id="button-divide" class="btn btn-danger operator" value="/"><h1>÷</h1></button> | |
| <button id="button-power" class="btn btn-danger operator" value="^"><h1>^</h1></button> | |
| <button id="button-equal" class="btn btn-success equal" value="="><h1>=</h1></button> | |
| <br><br> | |
| <button id="button-clear" class="btn btn-default clear" value="clear"><h1>clear</h1></button> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- Result Panel --> | |
| <div class="col-lg-6"> | |
| <div class="panel panel-default"> | |
| <div class="panel-heading"> | |
| <h3 class="panel-title">Result</h3> | |
| </div> | |
| <div class="panel-body"> | |
| <h1 id="first-number"></h1> | |
| <h1 id="operator"></h1> | |
| <h1 id="second-number"></h1> | |
| <hr> | |
| <h1 id="result"></h1> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <script type="text/javascript"> | |
| $(document).ready(function(){ | |
| const calculator = { | |
| firstNumber: "", | |
| secondNumber: "", | |
| operator: "", | |
| result: "", | |
| index: 0, | |
| elements: { | |
| firstNumberElement: $('#first-number'), | |
| secondNumberElement: $('#second-number'), | |
| operatorElement: $('#operator'), | |
| resultNumberElement: $('#result'), | |
| }, | |
| calculate() { | |
| this.result = eval(`${this.firstNumber} ${this.operator} ${this.secondNumber}`) | |
| }, | |
| clear() { | |
| this.firstNumber = '' | |
| this.secondNumber = '' | |
| this.operator = '' | |
| this.result = '' | |
| this.index = 0 | |
| this.update() | |
| }, | |
| update() { | |
| this.elements.firstNumberElement.text(this.firstNumber) | |
| this.elements.secondNumberElement.text(this.secondNumber) | |
| this.elements.operatorElement.text(this.operator) | |
| this.elements.resultNumberElement.text(this.result) | |
| } | |
| } | |
| $('.number').on('click', function() { | |
| if (calculator.index == 0) { | |
| calculator.firstNumber += $(this).attr('value') | |
| } else { | |
| calculator.secondNumber += $(this).attr('value') | |
| } | |
| calculator.update() | |
| }) | |
| $('.operator').on('click', function() { | |
| if (calculator.firstNumber) { | |
| // first number was set | |
| calculator.operator = $(this).attr('value') | |
| // allow mutating secondNumber | |
| calculator.index++ | |
| } else { | |
| // first number was not set | |
| alert('You havent set the first number!') | |
| } | |
| calculator.update() | |
| }) | |
| $('.equal').on('click', function() { | |
| calculator.calculate() | |
| calculator.update() | |
| }) | |
| $('.clear').on('click', function() { | |
| calculator.clear() | |
| }) | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment