-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
41 lines (31 loc) · 1.01 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
const btns = [...document.querySelectorAll('.btn')];
const listeKeyCode = btns.map(el=>el.dataset.key);
const screen = document.querySelector(".ecran");
document.addEventListener('keydown', (e) =>{
const value = e.keyCode.toString();
calc(value)
})
document.addEventListener('click', (e) =>{
const value = e.target.dataset.key;
calc(value)
})
const calc = (value) =>{
if (listeKeyCode.includes(value)) {
switch (value) {
case "8":
screen.textContent = "";
break;
case "13":
const calcul = eval(screen.textContent);
screen.textContent = calcul;
break;
default:
const indexKeyCode = listeKeyCode.indexOf(value);
const key = btns[indexKeyCode];
screen.textContent += key.innerHTML;
}
}
}
window.addEventListener('error', (e) => {
alert('Une erreur est survenue dans votre calcul')
})