-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcjs.js
63 lines (50 loc) · 1.77 KB
/
calcjs.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
$(document).ready(function(){
//variable to dispaly what buttons have been clicked
var $readOut = ""
//counter variable used to make sure 2 operators aren't entered in a row, every number added resets it to zero and operator added changes counter to 1 - samsies with the decimalcounter and decimalSameCounter
var $counter = 0;
var $decimalCounter = 0;
var $decimalSameNumber = 0;
var $equalCounter = 0;
$('.number').click(function(){
if ($readOut.length < 20) {
$readOut += $(this).attr('id');
$('.readoutText').html($readOut);
$counter = 0;
$decimalCounter = 0;}
})
//add operator and decimal to readout html but first check to make sure there aren't back-to-back operators/decimals
$('.calcbtn').click(function(){
if ($counter === 0 && $readOut.length > 0){
$readOut += $(this).attr('id');
$('.readoutText').html($readOut);
$counter = 1;
$decimalSameNumber = 0;
}
})
//clear button empties the readout html
$('#clear').click(function(){
$readOut = '';
$('.readoutText').html($readOut);
$decimalCounter = 0;
$decimalSameNumber = 0;
$counter = 0;
})
// make sure there are not 2 decimals in a row and make sure there aren't 2 decimals in the same number - reset counters when a operator is pressed to make sure not 2 in same number
$('.decimal').click(function(){
if ($decimalCounter === 0 && $decimalSameNumber === 0 ){
$readOut += $(this).attr('id');
$('.readoutText').html($readOut);
$decimalCounter = 1;
$decimalSameNumber = 1;
}
})
//calculate everything when equal button is pressed
$('#equal').click(function(){
$readOut = eval($readOut);
$('.readoutText').html($readOut);
$decimalCounter = 0;
$decimalSameNumber = 0;
$counter = 0;
})
}); // ends document ready