forked from J404Simpson/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpseudocode.js
85 lines (68 loc) · 1.84 KB
/
pseudocode.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
78
79
80
81
82
83
84
85
//Defie var etries as a mpt arra
//Defie var total as a mber
//Defie var temp as empt strig
//Eac click o a btto calls a fcti:
//Defie var for text to displa
//add nmbers to temp
//Clear we AC
//Clear last entr wit CE
//cage divide and mltipl smbols
//if = calclate
//ps nmber
var entries = [];
var total = 0;
var temp = '';
$("button").on('click', function() {
var val = $(this).text();
// Got a number, add to temp
if (!isNaN(val) || val === '.') {
temp += val;
$("#answer").val(temp.substring(0,10));
// Got some symbol other than equals, add temp to our entries
// then add our current symbol and clear temp
} else if (val === 'AC') {
entries = [];
temp = '';
total = 0;
$("#answer").val('')
// Clear last entry
} else if (val === 'CE') {
temp = '';
$("#answer").val('')
// Change multiply symbol to work with eval
} else if (val === 'x') {
entries.push(temp);
entries.push('*');
temp = '';
// Change divide symbol to work with eval
} else if (val === '÷') {
entries.push(temp);
entries.push('/');
temp = '';
// Got the equals sign, perform calculation
} else if (val === '=') {
entries.push(temp);
var nt = Number(entries[0]);
for (var i = 1; i < entries.length; i++) {
var nextNum = Number(entries[i+1])
var symbol = entries[i];
if (symbol === '+') { nt += nextNum; }
else if (symbol === '-') { nt -= nextNum; }
else if (symbol === '*') { nt *= nextNum; }
else if (symbol === '/') { nt /= nextNum; }
i++;
}
// Swap the '-' symbol so text input handles it correctly
if (nt < 0) {
nt = Math.abs(nt) + '-';
}
$("#answer").val(nt);
entries = [];
temp = '';
// Push number
} else {
entries.push(temp);
entries.push(val);
temp = '';
}
});