-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
47 lines (46 loc) · 1.7 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
41
42
43
44
45
46
47
let string="";
// let exponent=null;
let buttons=document.querySelectorAll('.button');
Array.from(buttons).forEach((button)=>{
button.addEventListener('click',(e)=>{
try{
if(e.target.innerHTML=='='){
string=eval(string); // evaluate to the input
document.querySelector('input').value=string;
}
else if(e.target.innerHTML=='c'){
string="" // clear the input
document.querySelector('input').value=string;
}
else if (e.target.innerHTML == '%') {
string = eval(string) / 100; // Convert the current expression to percentage
document.querySelector('input').value = string;
}
else if (e.target.innerHTML === '$') {
// Check if there is a valid expression before attempting to calculate
if (string !== "") {
let base = eval(string);
let exponent = prompt("Enter the exponent value");
// Check if exponent is a valid number
if (!isNaN(exponent) && exponent !== null && exponent !== "") {
exponent = parseFloat(exponent);
let result = Math.pow(base, exponent);
document.querySelector('input').value = result;
string = result.toString(); // Update the string with the result for further calculations if needed
} else {
alert("Please enter a valid number for the exponent.");
}
}
}
else{
console.log(e.target)
string=string+e.target.innerHTML;
document.querySelector('input').value=string;
}
}
catch(error){
console.error("error:",error.message)
document.querySelector('input').value=string;
}
})
})