-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
170 lines (146 loc) · 3.77 KB
/
script.js
File metadata and controls
170 lines (146 loc) · 3.77 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const currentEl = document.getElementById("current")
const prevEl = document.getElementById("prev")
const keys = document.querySelector('.keys')
// l'etat
let current = '0'
let previous = ''
let operator = null // 'plus' | 'minus' | 'multiply' | 'divide'
let evaluated = false;
// effectuer le calcul
const compute = (vA, vB, op) => {
const a = Number(vA);
const b = Number(vB);
switch (op) {
case 'plus': return (a + b).toString();
case 'minus': return (a - b).toString();
case 'multiply': return (a * b).toString();
case 'divide': return (b === 0 ? 'Erreur' : (a/b).toString());
default: return vB
}
}
// convertir l'affichage
const format = (numStr) => {
if (numStr === 'Erreur') return numStr;
const n = Number(numStr);
if(!isFinite(n)) return 'Erreur'
let out = n.toString();
if(out.length > 15) {
out = n.toExponential()
}
return out;
}
// actualiser l'affichage
const setDisplay = () => {
currentEl.textContent = format(current)
const operatorMap = {
plus: '+',
minus: '-',
multiply: 'x',
divide: '÷'
}
prevEl.textContent = operator && previous !== '' ? `${format(previous)} ${operatorMap[operator]}` : ''
}
// inserer un chiffre
const inputDigit = (d) => {
if(evaluated) {
current = '0';
evaluated = false;
return setDisplay();
}
if(current === '0') {
current = String(d);
} else {
current += String(d);
}
setDisplay()
}
// inserer un point
const inputDot = () => {
if(evaluated) {
current = '0.';
evaluated = false;
return setDisplay()
}
if(!current.includes('.')) {
current += '.'
}
setDisplay();
}
// choisir une opération
const chooseOperator = (op) => {
if(current == 'Erreur') return
if(previous !== '' && operator) {
const result = compute(previous, current, operator);
current = result;
previous == result === 'Erreur' ? '' : result;
} else {
previous = current;
}
operator = op;
evaluated = false;
current = '0'
setDisplay()
}
// tout effacer
const clearAll = () => {
current = '0';
previous = '';
operator = null;
evaluated = false;
setDisplay();
}
// effacer un caractere
const backspace = () => {
if(evaluated) return
if(current.length <= 1 || (current.length === 2 && current.startsWith('-'))) {
current = '0'
} else {
current = current.slice(0, -1);
}
setDisplay();
}
// égale
const equals = () => {
if(operator && previous !== '' && current !== 'Erreur') {
current = compute(previous, current, operator);
previous = '';
operator = null;
evaluated = true;
setDisplay();
}
}
// pourcentage
const percent = () => {
if(current == 'Erreur') return;
current = (Number(current) / 100).toString();
setDisplay();
}
// ecouter les boutons (clavier)
window.addEventListener('keydown', (e) => {
const k = e.key
if(/\d/.test(k)) return inputDigit(k);
if(k === '.') return inputDot();
if(k === '+') return chooseOperator('plus');
if(k === '-') return chooseOperator('minus');
if(k === '*') return chooseOperator('multiply');
if(k === '/') return chooseOperator('divide');
if(k === 'Enter' || k === '=') return equals();
if(k === 'Backspace') return backspace();
if(k.toLowerCase() === 'c') return clearAll();
})
// keys events (sur l'interface)
keys.addEventListener('click', (e) => {
const btn = e.target.closest('button');
if(!btn) return;
if(btn.dataset.num !== undefined) return inputDigit(btn.dataset.num)
if(btn.dataset.dot !== undefined) return inputDot();
if(btn.dataset.op !== undefined) return chooseOperator(btn.dataset.op);
const action = btn.dataset.action
switch (action) {
case 'clear': return clearAll();
case 'percent': return percent();
case 'backspace': return backspace();
case 'equals': return equals();
default: return;
}
})