-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
47 lines (38 loc) · 1.39 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 display = document.getElementById('display');
function appendToDisplay(value) {
display.value += value;
}
function clearDisplay() {
display.value = '';
}
function calculate() {
try {
const result = eval(display.value).toFixed(100);
display.value = result.replace(/(\.\d*?[1-9])?0*$/, '$1');
} catch (error) {
display.value = 'Error';
}
}
function backspace() {
display.value = display.value.slice(0, -1);
}
function calculateSquare() {
const currentValue = parseFloat(display.value);
display.value = (currentValue * currentValue).toFixed(100).replace(/(\.\d*?[1-9])?0*$/, '$1');
}
function calculateSquareRoot() {
const currentValue = parseFloat(display.value);
display.value = Math.sqrt(currentValue).toFixed(100).replace(/(\.\d*?[1-9])?0*$/, '$1');
}
function calculateCube() {
const currentValue = parseFloat(display.value);
display.value = (currentValue * currentValue * currentValue).toFixed(100).replace(/(\.\d*?[1-9])?0*$/, '$1');
}
function calculateCubeRoot() {
const currentValue = parseFloat(display.value);
display.value = Math.cbrt(currentValue).toFixed(100).replace(/(\.\d*?[1-9])?0*$/, '$1');
}
function calculatePercentage() {
const currentValue = parseFloat(display.value);
display.value = (currentValue / 100).toFixed(100).replace(/(\.\d*?[1-9])?0*$/, '$1');
}