-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
82 lines (71 loc) · 2.99 KB
/
app.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
let screen = document.querySelector('.screen');
document.addEventListener('transitionend', function (e) {
if (e.propertyName !== 'transform') return;
e.target.classList.remove('click');
});
document.addEventListener("click", function (e) {
if (e.target.classList.contains('clear')) { // digit;
e.target.classList.add('click');
screen.innerText = ''; // clear screen
}
if (e.target.classList.contains('digit')) { // digit;
e.target.classList.add('click');
screen.innerText += e.target.getAttribute('data-value'); // get value and append to screen
}
if (e.target.classList.contains('symbol')) { // digit
e.target.classList.add('click');
let screen_text = screen.innerText;
let symbol_value = e.target.getAttribute('data-value');
if (screen_text.length > 0) {
let last_value = screen_text.charAt(screen_text.length - 1);
if (symbol_value == '=') {
if (!isSymbol(last_value)) {
console.log('here')
symbol_position = screen_text.search(/[+|\-|/|*]/i);
let first_number = screen_text.substring(0, symbol_position);
let last_number = screen_text.substring(symbol_position + 1);
screen.innerText = calculate(first_number, last_number, screen_text[symbol_position]);
}
return;
}
if (isSymbol(last_value)) {
if (last_value.indexOf(symbol_value) == -1) {
screen.innerText = screen.innerText.replace(last_value, e.target.getAttribute('data-value'));
}
} else {
if (hasSymbol(screen_text)) {
symbol_position = screen_text.search(/[+|\-|/|*]/i);
let first_number = screen_text.substring(0, symbol_position);
let last_number = screen_text.substring(symbol_position + 1);
screen.innerText = calculate(first_number, last_number, screen_text[symbol_position]) + e.target.getAttribute('data-value');
} else {
screen.innerText += e.target.getAttribute('data-value'); // get value and append to screen
}
}
}
}
});
function isSymbol(sym) {
symbols = ['+', '-', '/', '*'];
return symbols.indexOf(sym) == -1 ? false : true;
}
function hasSymbol(text) {
return text.search(/[+|\-|/|*]/i) == -1 ? false : true;
}
function calculate(first_number, last_number, sym) {
switch (sym) {
case '+':
return parseFloat(first_number) + parseFloat(last_number);
break;
case '-':
return parseFloat(first_number) - parseFloat(last_number);
break;
case '/':
return parseFloat(first_number) / parseFloat(last_number);
break;
case '*':
return parseFloat(first_number) * parseFloat(last_number);
break;
}
return 0;
}