-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
120 lines (105 loc) · 3.68 KB
/
Copy pathscript.js
File metadata and controls
120 lines (105 loc) · 3.68 KB
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
116
117
118
119
120
let currentOperation = "";
let result=0;
function handleClick(button) {
if (button==="CE") {
currentOperation = "";
displayNumber("0");
return;
}
if (button === "+/-") {
if (currentOperation ==="" || currentOperation=== "0") return;
let operatorIndex =-1;
const operators = ["+","-","*","/","^"];
for (let i = currentOperation.length - 1;i >= 0;i--) {
if (operators.includes(currentOperation[i])) {
operatorIndex =i;
break;
}
}
if (operatorIndex ===-1) {
currentOperation= currentOperation.startsWith("-") ? currentOperation.slice(1) : "-" + currentOperation;
} else {
let beforeOperator = currentOperation.slice(0, operatorIndex + 1);
let number = currentOperation.slice(operatorIndex + 1);
number = number.startsWith("-") ? number.slice(1) : "-" + number;
currentOperation = beforeOperator + number;
}
displayNumber(currentOperation);
return;
}
if (button=== ".") {
let parts =currentOperation.split(/[\+\-\*\/\^]/);
let lastPart =parts[parts.length - 1];
if (!lastPart.includes(".")) {
currentOperation+=".";
displayNumber(currentOperation);
}
return;
}
const operators = ["+","-","*","/","^"];
if (operators.includes(button)) {
if (currentOperation==="" && button !=="-") return;
let lastChar =currentOperation[currentOperation.length -1];
if (operators.includes(lastChar)) return;
}
currentOperation =(currentOperation==="0") ? button : currentOperation+ button;
displayNumber(currentOperation);
}
function calculate() {
if (currentOperation=== "") return;
let operators = ["+","-","*","/","^"];
let operatorIndex = -1;
let operator ="";
for (let i= 1; i <currentOperation.length; i++) {
if (operators.includes(currentOperation[i])) {
operatorIndex= i;
operator= currentOperation[i];
break;
}
}
if (operatorIndex ===-1) {
displayNumber("Error");
return;
}
let a = Number(currentOperation.slice(0,operatorIndex));
let b = Number(currentOperation.slice(operatorIndex + 1));
if (isNaN(a) || isNaN(b)) {
displayNumber("Error");
return;
}
if (operator==="+") result= a+ b;
if (operator=== "-") result= a- b;
if (operator=== "*") result= a* b;
if (operator=== "/") {
if (b===0) {
displayNumber("Error");
currentOperation="";
return;
}
result=a/b;
}
if (operator==="^") result = a ** b;
displayNumber(result);
addToHistory(currentOperation, result);
currentOperation = result.toString();
}
function displayNumber(number) {
document.getElementById("screen").innerHTML= number;
}
function addToHistory(operation, result) {
let history =document.getElementById("history");
let historyItem =document.createElement("div");
historyItem.className ="history-item";
let operationText =document.createElement("div");
operationText.className ="history-operation";
operationText.innerHTML =operation;
let resultText =document.createElement("div");
resultText.className ="history-result";
resultText.innerHTML ="= "+ result;
historyItem.appendChild(operationText);
historyItem.appendChild(resultText);
history.prepend(historyItem);
}
function clearHistory() {
document.getElementById("history").innerHTML= "";
}