-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
115 lines (91 loc) · 2.97 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const screenDisplay = document.querySelector ("#div_screen")
const secondDisplay= document.querySelector ("#div_second_screen")
let numbers= document.querySelectorAll (".number")
const operators= document.querySelectorAll (".operator")
const equals= document.querySelector (".equals")
const clearcommand= document.querySelector (".clear")
const plusminuscommand = document.querySelector ("#plusminus")
const percentagecommand = document.querySelector ("#percentage")
let firstNum;
let secondNum;
let operation;
let screenDigits;
//display numbers on screen
numbers.forEach((numb)=> {
numb.addEventListener ("click", (event) => {
if (screenDisplay.innerHTML==operation) {
screenDisplay.innerHTML= "";
}
// limit screen display to 10 digits for first and second numbers.
screenDisplay.innerHTML += event.target.value
if (screenDisplay.innerHTML.length >= 9) {
event.target.value= "" }
})})
// display operators + firstnumber
operators.forEach ((ops) => {
ops.addEventListener ("click", (event) => {
operation = event.target.value;
firstNum= screenDisplay.innerHTML
secondDisplay.innerHTML= (firstNum) + " " + (operation);
screenDisplay.innerHTML=operation;
})})
// now we need to add second number
const calculateResult= (operation) => {
if ( operation == "+") {
return parseFloat(firstNum) + parseFloat(secondNum)
} else if ( operation == "-") {
return parseFloat(firstNum) - parseFloat(secondNum)
} else if (operation == "÷") {
return parseFloat (firstNum) / parseFloat (secondNum)
}
else if (operation == "x") {
return parseFloat(firstNum) * parseFloat (secondNum)
} else if (operation== "") {
return firstNum
}
}
equals.addEventListener("click", () => {
secondNum = screenDisplay.innerHTML
if (screenDisplay.innerHTML== "") {
secondDisplay.innerHTML= ""
} else if (secondNum==operation) {
screenDisplay.innerHTML=""
} else {
secondDisplay.innerHTML= (firstNum) + " " + (operation) + " " + (secondNum)
screenDisplay.innerHTML= calculateResult(operation)
}
})
//clear button
clearcommand.addEventListener ("click", () =>
{
screenDisplay.innerHTML= "";
firstNum="";
secondNum="";
secondDisplay.innerHTML = "";
operation="";
})
// plusminus
const plusMinusFunction = () => {
if (screenDigits > 0) {
return screenDigits * (-1)
} else if (screenDigits < 0) {
return screenDigits * (1)
}
}
plusminuscommand.addEventListener ("click", () => {
screenDigits = screenDisplay.innerHTML;
screenDisplay.innerHTML= plusMinusFunction();
})
// plusminuscommand.addEventListener ("click", () => {
// screenDigits = screenDisplay.innerHTML;
// if (screenDigits > 0) {
// screenDisplay.innerHTML= screenDigits * (-1)
// } else if (screenDigits < 0) {
// screenDisplay.innerHTML = screenDigits * (1)
// }
// })
// percentages /100
percentagecommand.addEventListener ("click", () => {
screenDigits = screenDisplay.innerHTML;
screenDisplay.innerHTML= screenDigits / (100)}
)