- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
Latest commit
78 lines (64 loc) · 2.3 KB
/
Copy pathscript.js
File metadata and controls
78 lines (64 loc) · 2.3 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
document.addEventListener('DOMContentLoaded',function(){
constdisplay=document.getElementById('display');
constbuttons=document.querySelectorAll('button');
functionupdateDisplay(text){
display.value+=text;
}
functionhandleNumberClick(number){
updateDisplay(number);
}
functionhandleOperatorClick(operator){
updateDisplay(operator);
}
functionhandleCalculateClick(){
try{
letexpression=display.value;
// Replace '×' with '*' for multiplication
expression=expression.replace(/×/g,'*');
// Replace (a)(b) with (a*b)
expression=expression.replace(/\((\d+)\)\((\d+)\)/g,'($1*$2)');
// Replace (a)b with (a*b)
expression=expression.replace(/\((\d+)\)(\d+)/g,'($1*$2)');
// Replace (expression)(expression) with (expression*expression)
expression=expression.replace(/\(([^)]+)\)\(([^)]+)\)/g,'($1*$2)');
// Replace (expression)expression with (expression*expression)
expression=expression.replace(/\(([^)]+)\)([^)]+)/g,'($1*$2)');
constresult=evaluateExpression(expression);
display.value=result;
}catch(error){
display.value='Error';
}
}
functionhandleClearClick(){
display.value='';
}
functionhandleDeleteClick(){
display.value=display.value.slice(0,-1);
}
functionevaluateExpression(expression){
// Evaluate the expression using the eval function
returneval(expression);
}
buttons.forEach(function(button){
button.addEventListener('click',function(){
constaction=button.getAttribute('data-action');
if(action==='append'){
consttext=button.textContent;
handleNumberClick(text);
}elseif(action==='add'||action==='subtract'||action==='multiply'||action==='divide'){
constoperator=button.textContent;
handleOperatorClick(operator);
}elseif(action==='calculate'){
handleCalculateClick();
}elseif(action==='clear'){
handleClearClick();
}elseif(action==='delete'){
handleDeleteClick();
}elseif(action==='open-parenthesis'){
updateDisplay('(');
}elseif(action==='close-parenthesis'){
updateDisplay(')');
}
});
});
});