-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculatrice.js
63 lines (54 loc) · 1.61 KB
/
calculatrice.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
let result = '', operator = '', number1 = '', number2 = '';
function getNumber(number) {
if (result !== '') {
number1 = '';
result = '';
}
operator === '' ? number1 += number : number2 += number;
displayOperation();
}
function getOperator(operatorCapture) {
if (number1 === '') {
errorMessage("On commence par les chiffres");
}
operator !== '' ? errorMessage("Calcul à un seul opérateur") : operator += operatorCapture;
displayOperation();
}
function setRelative() {
operator === '' ? number1 = changeNumber(number1) : number2 = changeNumber(number2);
displayOperation();
}
function changeNumber(number) {
return number.charAt(0) === '-' ? number.slice(1) : '-' + number;
}
function getResult() {
if (operator === '') {
errorMessage('Il faut saisir une opération');
displayConsole();
} else {
result = eval(number1 + ' ' + operator + ' ' + number2);
document.getElementById('display').innerHTML = result;
reset();
number1 = result.toString();
}
}
function displayConsole (){
console.log('number1 : ' + number1);
console.log('number2 : ' + number2);
console.log('operator : ' + operator);
console.log('result : ' + result);
}
function reset() {
operator = '';
number1 = '';
number2 = '';
}
function errorMessage(message) {
document.getElementById('display').innerHTML = message;
setTimeout(function () {
displayOperation();
}, 2000);
}
function displayOperation() {
document.getElementById('display').innerHTML = number1 + ' ' + operator + ' ' + number2;
}