-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculator.js
126 lines (111 loc) · 3.75 KB
/
calculator.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// JavaScript Calculator Code with Extended Functionality
// Calculator state object
const calculatorState = {
displayValue: '0',
memoryValue: null,
operator: null,
};
// Function to update the display
function updateDisplay() {
document.getElementById('display').value = calculatorState.displayValue;
}
// Function to clear the calculator state
function clearCalculator() {
calculatorState.displayValue = '0';
calculatorState.memoryValue = null;
calculatorState.operator = null;
updateDisplay();
}
// Function to append values to the display
function appendToDisplay(value) {
if (calculatorState.displayValue === '0' || calculatorState.displayValue === 'Error') {
calculatorState.displayValue = value;
} else {
calculatorState.displayValue += value;
}
updateDisplay();
}
// Function to handle decimal point
function handleDecimal() {
if (!calculatorState.displayValue.includes('.')) {
calculatorState.displayValue += '.';
updateDisplay();
}
}
// Function to handle memory operations
function handleMemoryOperation(operation) {
const currentDisplay = parseFloat(calculatorState.displayValue);
switch (operation) {
case 'MC':
calculatorState.memoryValue = null;
break;
case 'MR':
if (calculatorState.memoryValue !== null) {
calculatorState.displayValue = calculatorState.memoryValue.toString();
}
break;
case 'M+':
calculatorState.memoryValue = (calculatorState.memoryValue || 0) + currentDisplay;
break;
case 'M-':
calculatorState.memoryValue = (calculatorState.memoryValue || 0) - currentDisplay;
break;
default:
break;
}
updateDisplay();
}
// Function to perform percentage calculation
function calculatePercentage() {
const currentDisplay = parseFloat(calculatorState.displayValue);
calculatorState.displayValue = (calculatorState.memoryValue || 0) + (currentDisplay / 100);
updateDisplay();
}
// Function to perform square root calculation
function calculateSquareRoot() {
const currentDisplay = parseFloat(calculatorState.displayValue);
calculatorState.displayValue = Math.sqrt(currentDisplay).toString();
updateDisplay();
}
// Function to perform basic arithmetic operations
function performOperation(nextOperator) {
const inputValue = parseFloat(calculatorState.displayValue);
if (calculatorState.operator && calculatorState.memoryValue !== null) {
switch (calculatorState.operator) {
case '+':
calculatorState.memoryValue += inputValue;
break;
case '-':
calculatorState.memoryValue -= inputValue;
break;
case '*':
calculatorState.memoryValue *= inputValue;
break;
case '/':
if (inputValue !== 0) {
calculatorState.memoryValue /= inputValue;
} else {
calculatorState.displayValue = 'Error';
updateDisplay();
return;
}
break;
default:
break;
}
} else {
calculatorState.memoryValue = inputValue;
}
calculatorState.displayValue = calculatorState.memoryValue.toString();
calculatorState.operator = nextOperator;
updateDisplay();
}
// Example usage:
// clearCalculator(); // Clear the calculator
// appendToDisplay('5'); // Append '5' to the display
// handleDecimal(); // Add decimal point
// handleMemoryOperation('M+'); // Add to memory
// calculatePercentage(); // Calculate percentage
// calculateSquareRoot(); // Calculate square root
// performOperation('+'); // Perform addition
// performOperation('='); // Perform equals operation