-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
50 lines (43 loc) · 1.24 KB
/
script.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
let insert = document.querySelector(".insert__value");
let result = document.querySelector(".result__value");
let buttons = document.querySelectorAll(".data");
let cancel = document.querySelector(".cancel");
let clear = document.querySelector(".clear");
let negative = document.querySelector(".negative");
let equal = document.querySelector(".equal");
buttons.forEach(button => {
button.addEventListener('click', function(e) {
let value = e.target.dataset.num;
let qtdNumbers = insert.value;
if (qtdNumbers.length < 25) {
insert.value += value;
}
})
});
cancel.addEventListener('click', () => {
let lastNumber = insert.value.slice(0, insert.value.length - 1);
insert.value = lastNumber;
});
clear.addEventListener('click', () => {
insert.value = "";
result.value = "";
});
equal.addEventListener('click', () => {
if(insert.value === '') {
result.value = "";
}
else {
let answer = eval(insert.value);
result.value = answer;
}
});
negative.addEventListener('click', () => {
if (Math.sign(insert.value) === 1) {
let insertNegative = `-${insert.value}`
insert.value = insertNegative;
}
else {
let insertPositive = insert.value.replace('-', '');
insert.value = insertPositive;
}
});