-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.calc.js
77 lines (73 loc) · 2.2 KB
/
jquery.calc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* jQuery Growl Calc
* Version 2.0.0
* Last Updated 2014-02-08
* @requires jQuery v1.11.0 or later (untested on previous version)
*
* Examples at: http://projects.zoulcreations.com/jquery/jquery-calc/
* Copyright (c) 2008-2014 David Higgins
*
*/
jQuery(function($) {
var digits = $('#calculator .digits .digit');
var clear = $('#calculator .clear');
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 = calculator.result + '';
rightBuffer = '';
}
function isDigit(key) {
var digits = "0123456789";
var digit = false;
if(digits.indexOf(key) != -1)
digit = true;
return digit;
}
var leftBuffer = '';
var rightBuffer = '';
clear.click(function() {
leftBuffer = '';
rightBuffer = '';
calculator = { left: false, right: false, result: 0, operator: '+' };
$('#calculator .result').text(calculator.result);
});
digits.click(function() {
var key = $(this).text();
console.log(leftBuffer, rightBuffer);
if(isDigit(key)) {
if(calculator.left) {
rightBuffer += key.toString();
$('#calculator .result').text(rightBuffer);
} else {
leftBuffer += key.toString();
$('#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 == '.') {
if(calculator.left) {
rightBuffer += '.';
$('#calculator .result').text(rightBuffer);
} else {
leftBuffer += '.';
$('#calculator .result').text(leftBuffer);
}
} else {
calculator.right = rightBuffer;
if(calculator.left.substring(calculator.left.length-1, 1) == '.')
calculator.left += '0';
if(calculator.right.substring(calculator.right.length-1, 1) == '.')
calculator.right += '0';
calculate();
}
});
});