Skip to content

Commit

Permalink
fixed mul/div and added continuous calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
zoul0813 committed Feb 8, 2014
1 parent 73c3d12 commit 7a351b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
font-size: 2em;
}
</style>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<div class="container">
Expand Down Expand Up @@ -70,7 +71,6 @@ <h1>jQuery Calculator</h1>
</div>

<script src="jquery.calc.js" type="text/javascript"></script>
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script src="jquery.calc.js"></script>
Expand Down
11 changes: 8 additions & 3 deletions jquery.calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
*
*/

$(document).ready(function() {
jQuery(function($) {
var digits = $('#calculator .digits .digit');
var calculator = { left: false, right: false, result: 0, operator: '+' };
function calculate() {
calculator.result = eval(calculator.left + calculator.operator + calculator.right);
$('#calculator .result').text(calculator.result);
calculator.left = null;
calculator.right = null;
leftBuffer = '';
leftBuffer = calculator.result + '';
rightBuffer = '';
}
function isDigit(key) {
Expand All @@ -31,7 +31,8 @@ $(document).ready(function() {
var rightBuffer = '';

digits.click(function() {
var key = $(this).text();;
var key = $(this).text();
console.log(leftBuffer, rightBuffer);
if(isDigit(key)) {
if(calculator.left) {
rightBuffer += key.toString();
Expand All @@ -41,6 +42,10 @@ $(document).ready(function() {
$('#calculator .result').text(leftBuffer);
}
} else if(key != '=' && key != '.') {
switch(key) {
case '÷': key = '/'; break;
case '×': key = '*'; break;
}
calculator.operator = key;
calculator.left = leftBuffer;
} else if(key == '.') {
Expand Down

0 comments on commit 7a351b1

Please sign in to comment.