-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
136 lines (104 loc) · 3.4 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
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
const display = document.querySelector('.screen');
const keys = document.querySelector('.container1');
let displayValue = '0'; //screen de çıkacak değeri tanımladık başlangıç değeri 0
let firstValue = null; // first value to given first value memory
let operator = null; // for after first value memory
let secondValue = false; // for after second value memory if there is any second value equals to true in a function
updateDisplay();
function updateDisplay(){
display.value = displayValue;
}
///**********part1 definition of buttons */
keys.addEventListener('click', function(event){
const element = event.target;
if (!element.matches('button')) return; // button olmayan elemanları çalıştırma
if (element.classList.contains('operator')){
// console.log('operator', element.value);
inputOperator(element.value);
updateDisplay();
return;
} // operatörlerin tanımlanması
if (element.classList.contains('clear')){
// console.log('clear', element.value);
inputClear();
updateDisplay();
return;
} // clear tanımlanması
if (element.classList.contains('negative')){
// console.log('negative', element.value);
inputNegative();
updateDisplay();
return;
} // to negative definition
if (element.classList.contains('percentage')){
// console.log('percentage', element.value);
inputPercentage();
updateDisplay();
return;
} // opercentage definition
if (element.classList.contains('decimal')){
// console.log('decimal', element.value);
inputDecimal();
updateDisplay();
return;
} // decimal definition
// if (element.classList.contains('equal')){
// console.log('equal', element.value);
// return;
// } // equal definition
inputNumber(element.value);
updateDisplay();
}); // tuşların sadece geçerli olması için yapıldı
//******** printing the values of the keys in the input part */
function inputNumber(num){
if(secondValue){
displayValue = num;
secondValue = false;
} else{
displayValue = displayValue === '0' ? num: displayValue + num; //if display value equals to 0 write the number othervise write the other side by other number
}
console.log(displayValue, firstValue, operator, secondValue);
}
function inputDecimal(){
if(!displayValue.includes('.')){
displayValue += "."
}
}
function inputClear(){
displayValue = '0';
}
function inputNegative(){
displayValue = displayValue*-1;
}
function inputPercentage(){
displayValue = displayValue/100;
}
function inputOperator(nextOperator){
const value = parseFloat(displayValue);
if(operator && secondValue){
operator = nextOperator;
return;
}
if (firstValue === null) {
firstValue = value;
} else if (operator){
const result = calculate(firstValue, value, operator);
displayValue = `${parseFloat(result.toFixed(2))}`;
firstValue = result;
}
secondValue = true;
operator = nextOperator;
console.log(displayValue, firstValue, operator, secondValue);
}
function calculate (first, second, operator){
if(operator === '+'){
return first + second;
} else if(operator === '-'){
return first - second;
} else if(operator === 'x'){
return first * second;
} else if(operator === '/'){
return first / second;
}
return second;
}