-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (59 loc) · 2.51 KB
/
Copy pathscript.js
File metadata and controls
67 lines (59 loc) · 2.51 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
let screen = document.getElementById('screen');
buttons = document.querySelectorAll('button');
let screenValue = '';
for (item of buttons) {
item.addEventListener('click', (e) => {
buttonText = e.target.innerText;
console.log('Button text is ', buttonText);
//when 'x' button is pressed, '*' is shown on calculator screen
if (buttonText == 'x') {
buttonText = '*';
screenValue += buttonText;
screen.value = screenValue;
}
/*when 'Sq.' button is pressed, the square of the number present on screen
is displayed in the place of the original number*/
else if (buttonText == 'Sq.') {
buttonText = Math.pow(screenValue, 2);
screenValue = buttonText;
screen.value = screenValue;
}
/*when 'Cu.' button is pressed, the cube of the number present on screen
is displayed in the place of the original number*/
else if (buttonText == 'Cu.') {
buttonText = Math.pow(screenValue, 3);
screenValue = buttonText;
screen.value = screenValue;
}
/*when 'sqrt' button is pressed, the square root of the number present on screen
is displayed in the place of the original number*/
else if (buttonText == 'sqrt') {
buttonText = Math.sqrt(screenValue);
screenValue = buttonText;
screen.value = screenValue;
}
/*when 'cbrt' button is pressed, the cube root of the number present on screen
is displayed in the place of the original number*/
else if (buttonText == 'cbrt') {
buttonText = Math.cbrt(screenValue);
screenValue = buttonText;
screen.value = screenValue;
}
//when 'AC' button is pressed, the calculator screen is cleared completely
else if (buttonText == 'AC') {
screenValue = "";
screen.value = screenValue;
}
/*when '=' button is pressed, the equation on the calculator screen is
evaluated and the result is displayed in its place*/
else if (buttonText == '=') {
screen.value = eval(screenValue);
}
/*when any other button is pressed, the value on the pressed button is displayed
next to the existing equation on the calculator screen*/
else {
screenValue += buttonText;
screen.value = screenValue;
}
})
}