-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal.js
34 lines (32 loc) · 1.06 KB
/
cal.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
const btn = document.querySelectorAll("button");
const input = document.querySelector("input");
const special = ["%","*","/","+","=","-"];
let output = "";
//Calculate function to calculate on the basis of button clicked
const calculate = (btnValue) =>{
input.focus();
if (btnValue === "="&& output != "") {
//If the button "%" Clicked then perform "/100"
output = eval(output.replace("%","/100"));
}
else if (btnValue === "AC") {
output = "";
}
else if (btnValue === "DEL") {
//If "DEL" Clicked then delete last wrriten number
output = output.toString().slice(0,- 1);
}
else{
//If output is empty and special clicked then return
if (output === "=" && special.includes(btnValue) ) {
return;
}
output += btnValue;
}
input.value = output;
};
//add event listner to button to Calculate()
btn.forEach((button)=>{
//click listner call calculate() with dataset as the base Value
button.addEventListener("click",(e) => calculate(e.target.dataset.value));
})