-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
139 lines (139 loc) · 4.3 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//Create a Class for our calculator
class Calculator {
//create a constructor
constructor(c, p) {
this.currentTextElement = c;
this.previousTextElement = p;
this.clear();
}
//clear the result
clear() {
this.currentValue = "";
this.previousValue = "";
}
//delete the last number
delete() {
this.currentValue = this.currentValue.toString().slice(0, -1);
}
//append the number
appendNumber(btnValue) {
//check if the last character is an operator or already a dot
if (btnValue === "." && this.currentValue.includes(".")) {
alert("you can't add more than one dot");
return;
}
this.currentValue += btnValue;
}
//choose the operator
chooseOperator(operator) {
if (this.currentValue === "") return;
if (this.previousValue !== "") this.calculate();
this.operator = operator;
this.previousValue = this.currentValue;
this.currentValue = "";
}
//calculate the result
calculate() {
let result;
const p = parseFloat(this.previousValue);
const c = parseFloat(this.currentValue);
if (isNaN(p) || isNaN(c)) return;
switch (this.operator) {
case "+":
result = p + c;
break;
case "-":
result = p - c;
break;
case "*":
result = p * c;
break;
case "/":
result = p / c;
break;
default:
return;
}
this.currentValue = result;
this.operator = "";
this.previousValue = "";
}
//display formater
numberFormater(number) {
//convert the number to string
const stringNumber = number.toString();
//split the number into integer and decimal [0] is integer and [1] is decimal
const integerDigits = parseFloat(stringNumber.split(".")[0]);
const decimalDigits = stringNumber.split(".")[1];
//Example:100.50=> int: 100 |.| decimal: 50
//declare a variable to store the integer
let myInteger;
//check if the integer is a number
if (isNaN(integerDigits)) {
myInteger = "";
} else {//if it is a number then format it
//convert the integer to a string and add a comma after every 3 digits
myInteger = integerDigits.toLocaleString("en", {
maximumFractionDigits: 0,//set the maximum number of decimal digits
});
}
//check if the decimal is a number
if (decimalDigits != null) {
return `${myInteger}.${decimalDigits}`;
} else {//if it is not a number then return the integer
return myInteger;
}
}
//update the display
updateDisplay() {
this.currentTextElement.innerText = this.numberFormater(this.currentValue);
if (this.operator != "" && this.previousValue != "")
this.previousTextElement.innerText = `${this.numberFormater(
this.previousValue
)} ${this.operator}`;
else
this.previousTextElement.innerText = this.numberFormater(
this.previousValue
);
}
}
//get the value each item in our calculator
const clearAllBtn = document.querySelector(".item-all-clear");
const deleteBtn = document.querySelector(".item-delete");
const equalBtn = document.querySelector(".item-operator-equal");
const numberBtns = document.querySelectorAll(".item-number");
const operatorBtns = document.querySelectorAll(".item-operator");
const currentTextElement = document.querySelector(".current-value");
const previousTextElement = document.querySelector(".previous-value");
//create a new instance of our calculator
const calculator = new Calculator(currentTextElement, previousTextElement);
//add event listener to each button
//add event listener to each number
numberBtns.forEach((btn) =>
btn.addEventListener("click", () => {
calculator.appendNumber(btn.innerText);
calculator.updateDisplay();
})
);
//add event listener to each operator
operatorBtns.forEach((btn) =>
btn.addEventListener("click", () => {
calculator.chooseOperator(btn.innerText);
calculator.updateDisplay();
})
);
//add event listener to the equal button
equalBtn.addEventListener("click", () => {
calculator.calculate();
calculator.updateDisplay();
});
//add event listener to the clear and delete button
clearAllBtn.addEventListener("click", () => {
calculator.clear();
calculator.updateDisplay();
});
//add event listener to the delete button
deleteBtn.addEventListener("click", () => {
calculator.delete();
calculator.updateDisplay();
});