-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathcalculator.js
50 lines (47 loc) · 1.33 KB
/
calculator.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
document.addEventListener('DOMContentLoaded', startCalc)
var inputs = ['']
var operators = ['+', '-', '/', '*']
var totalString
var decimal = ['.']
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
var lastOutput = ['']
function startCalc() {
function getValue(string) {
if (string.includes(inputs[inputs.length]) === true && string === '.') {
console.log('Duplicate Decimal')
} else if (inputs.length === 1 && operators.includes(string) === false) {
inputs.push(string)
} else if (operators.includes(inputs[inputs.length - 1]) === false) {
inputs.push(string)
} else if (numbers.includes(Number(string))) {
inputs.push(string)
}
update()
}
function update() {
totalString = inputs.join('')
$('#equation').html(totalString)
console.log(inputs)
}
function getTotal() {
totalString = inputs.join('')
$('#output').html(totalString)
$('#equation').html(eval(totalString))
}
$('button').on('click', function() {
if (this.id === 'Clear_Entry') {
$('#output').html('')
$('#equation').html('0')
totalString = null
inputs = ['']
} else if (this.id === 'total') {
getTotal()
} else {
if (inputs[inputs.length - 1].indexOf('+', '-', '/', '*', '.') === -1) {
getValue(this.id)
} else {
getValue(this.id)
}
}
})
}