-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (90 loc) · 3.87 KB
/
index.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
const operators = document.querySelectorAll('.num');
const operator =document.querySelectorAll('.operator');
const exp= document.querySelector('.expression');
const eval = document.querySelector('.egale');
const resultat = document.querySelector('.result');
const clearBtn = document.querySelector('.clear');
let expressionArray=[];
operator.forEach(op =>{
op.addEventListener('click',SauvOperator)
});
operators.forEach(num =>{
num.addEventListener('click', SauvNumber);
});
eval.addEventListener('click', evaluateExpression)
clearBtn.addEventListener('click',clearDisplay);
function SauvNumber(event){
const number = event.target.innerHTML;
addToArray(number);
}
function SauvOperator (event){
const operator =event.target.innerHTML;
addToArray(operator);
}
function addToArray(item){
let l = expressionArray.length;
console.log(l);
if(l==0){
if(item.trim()=== '+'.trim() || item.trim()=== '/'.trim()|| item.trim()=== '-'.trim()||item.trim()=== '*'.trim()){
alert("you can't start an arithmetique formula with an operator !!");
}else{
expressionArray.push(item);
exp.innerHTML=expressionArray.join("");
}
}
if(l>0){
if(item.trim()=== '0'.trim() || item.trim()=== '1'.trim() || item.trim()=== '2'.trim()|| item.trim()=== '3'.trim()||item.trim()=== '4'.trim() ||item.trim()=== '5'.trim() ||item.trim()=== '6'.trim() ||item.trim()=== '7'.trim() ||item.trim()=== '8'.trim() ||item.trim()=== '9'.trim()){
if(expressionArray[l-1].trim()==='+'.trim() || expressionArray[l-1].trim()==='/'.trim() || expressionArray[l-1].trim()==='*'.trim() || expressionArray[l-1].trim()==='-'.trim()){
expressionArray.push(item);
exp.innerHTML=expressionArray.join("");
}else{ alert(" You sould repect the arithmetique Formula 3"); }
}else{
if(item.trim()=== '+'.trim() || item.trim()=== '/'.trim()|| item.trim()=== '-'.trim()||item.trim()=== '*'.trim()) {
if(expressionArray[l-1].trim()=== '0'.trim() ||expressionArray[l-1].trim()=== '1'.trim() || expressionArray[l-1].trim()=== '2'.trim()|| expressionArray[l-1].trim()=== '3'.trim()||expressionArray[l-1].trim()=== '4'.trim() ||expressionArray[l-1].trim()=== '5'.trim() ||expressionArray[l-1].trim()=== '6'.trim() ||expressionArray[l-1].trim()=== '7'.trim() ||expressionArray[l-1].trim()=== '8'.trim() ||expressionArray[l-1].trim()=== '9'.trim())
{
expressionArray.push(item);
exp.innerHTML=expressionArray.join("");
}else
{
alert(" You sould repect the arithmetique Formula ");
}
}
}
}
}
function evaluateExpression(){
let l=expressionArray.length;
if(expressionArray[0].trim()!= '+'.trim() && expressionArray[0].trim()!= '/'.trim() && expressionArray[0].trim()!= '-'.trim() && expressionArray[0].trim() != '*'.trim() ){
let solution =Number(expressionArray[0]);
for(var i=1 ; i<l;i++){
switch (expressionArray[i].trim()){
case '+' :{
solution+= Number(expressionArray[i+1]);
break;
}
case '-':{
solution -= Number(expressionArray[i+1]);
break;
}
case '*':{
solution *= Number(expressionArray[i+1]);
break;
}
case '/':{
solution /= Number(expressionArray[i+1]);
break;
}
default:continue;
}
resultat.innerHTML=solution;
}
}else{
alert("Invalid formula : cannot finish with an operator");
}
}
function clearDisplay(){
resultat.innerHTML="";
exp.innerHTML="";
expressionArray=[];
console.log(expressionArray);
}