A modern, feature-rich scientific calculator built with HTML, CSS, and JavaScript that performs basic arithmetic, trigonometric, logarithmic, and advanced mathematical operations with full BODMAS/PEMDAS support.
- BODMAS/PEMDAS Support: Correctly evaluates complex expressions following order of operations
- Expression Building: Build and visualize complete mathematical expressions before calculating
- Mode Toggle: Switch between Scientific (5-column) and Standard (4-column) calculator modes with ⇄ button
- Scientific Mode:
- Trigonometric Functions: Sine, Cosine, Tangent with degree/radian modes and inverse functions
- Logarithmic Functions: Common (lg) and Natural (ln) logarithms
- Advanced Operations: Power (x^y), Factorial (x!), Reciprocal (1/x), Square root (√)
- Mathematical Constants: Pi (π) and Euler's number (e)
- Parentheses Support: ( and ) for complex expressions
- 2nd Function: Toggle for accessing inverse trigonometric functions
- Standard Mode: Simple 4-column layout with essential arithmetic operations
- Arithmetic Operations: Addition (+), Subtraction (−), Multiplication (×), and Division (÷)
- Percentage Calculations: Simple percentage conversion (divides by 100)
- Calculation History: View, recall, and export past calculations with timestamps
- Theme Customization: Toggle between light and dark themes with persistent preference
- Export Functionality: Export calculation history as CSV file
- Clean User Interface: Modern, responsive design that works on desktop and mobile devices
- Error Handling: Comprehensive validation for all operations
- Decimal Support: Perform calculations with decimal numbers
- Keyboard Support: Use your keyboard for faster input with proper operator symbols
- Visual Feedback: Button hover and click animations for better user experience
- Expression Display: Shows the complete expression being built in real-time
- HTML5: Structure and semantic markup
- CSS3: Styling with modern features (Grid, Flexbox, gradients, transitions)
- JavaScript (ES6+): Calculator logic and DOM manipulation
calculator/
├── index.html # Main HTML structure
├── styles.css # Styling and layout
├── script.js # Calculator functionality
└── README.md # Documentation (this file)
- Navigate to the
calculatordirectory - Open
index.htmlin your web browser - Alternatively, you can serve it with a local web server:
# Using Python 3 python -m http.server 8000 # Using Node.js (if http-server is installed) npx http-server
- Open your browser and navigate to
http://localhost:8000
- Numbers: Click on number buttons (0-9) to enter numbers
- Decimal Point: Click the
.button to add decimal points - Operators: Click
+,−,×, or÷to select an operation - Trigonometric Functions:
- Click
sin,cos, ortanfor trigonometric functions - Click
2ndbutton first, then trig function for inverse (arcsin, arccos, arctan) - Click
degto toggle between degree and radian mode
- Click
- Logarithmic Functions:
- Click
lgfor common logarithm (base 10) - Click
lnfor natural logarithm (base e)
- Click
- Advanced Functions:
- Click
√to calculate square root - Click
x^yto raise a number to a power - Click
x!for factorial - Click
1/xfor reciprocal - Click
%for percentage calculations
- Click
- Constants & Parentheses:
- Click
πto insert Pi (3.14159...) - Click
eto insert Euler's number (2.71828...) - Click
(and)for parentheses
- Click
- Memory Function:
- Click
MRto recall memory value
- Click
- Equals: Click
=to calculate the result - Clear (AC): Click
ACto clear all and reset the calculator - Delete (DEL): Click
DELto remove the last digit - Theme Toggle: Click the moon/sun icon to switch between dark and light themes
- History:
- Click any history item to recall that result
- Click
Exportto download history as CSV - Click
Clearto remove all history
- Numbers: Press
0-9keys - Decimal Point: Press
.key - Operators: Press
+,-,*, or/keys - Calculate: Press
Enteror=key - Clear: Press
Escapekey - Delete: Press
Backspacekey
-
Simple Addition:
- Click
5→+→3→= - Expression displays:
5 + 3 - Result:
8
- Click
-
BODMAS Order of Operations:
- Click
5→+→3→×→2→= - Expression displays:
5 + 3 × 2 - Result:
11(multiplication happens first: 3 × 2 = 6, then 5 + 6 = 11)
- Click
-
Complex Expression with Parentheses:
- Click
(→5→+→3→)→×→2→= - Expression displays:
(5 + 3) × 2 - Result:
16(parentheses first: 5 + 3 = 8, then 8 × 2 = 16)
- Click
-
Decimal Operations:
- Click
10.5→×→2→= - Result:
21
- Click
-
Division by Zero (Error Handling):
- Click
5→÷→0→= - Result: Error message "Invalid calculation"
- Click
-
Square Root (Unary Operation):
- Click
25→√ - Result:
5(immediately calculated, no equals needed)
- Click
-
Percentage:
- Click
50→% - Result:
0.5(50 ÷ 100)
- Click
-
Power Operation:
- Click
2→x^y→8→= - Expression displays:
2 ^ 8 - Result:
256(2⁸)
- Click
-
Trigonometric Function:
- Ensure
degmode is selected - Click
30→sin - Result:
0.5(immediately calculated)
- Ensure
-
Changing Operators:
- Click
5→+→×(replaces + with ×) →3→= - Expression displays:
5 × 3 - Result:
15
- Click
- Real-time Expression Display: See your complete mathematical expression as you build it
- BODMAS/PEMDAS Evaluation: Correctly follows order of operations:
- Brackets/Parentheses
( ) - Orders/Exponents
^ - Division
÷and Multiplication×(left to right) - Addition
+and Subtraction−(left to right)
- Brackets/Parentheses
- Operator Replacement: Change your mind? Press a different operator to replace the last one
- Expression Persistence: The expression stays visible until you press equals
- Smart Parentheses: Build complex nested expressions with visual feedback
- Example:
5 + 3 × 2correctly evaluates to11, not16
-
Unary Operations (execute immediately, no equals needed):
- Square root
√ - Factorial
x! - Reciprocal
1/x - Trigonometric functions (sin, cos, tan)
- Logarithms (lg, ln)
- Percentage
% - These clear any pending expression and show the result immediately
- Square root
-
Binary Operations (build expressions):
- Arithmetic operators
+,−,×,÷ - Power
x^y - These add to the expression and wait for equals
=
- Arithmetic operators
-
Trigonometric Functions:
- sin, cos, tan: Calculate trigonometric ratios
- Degree/Radian Mode: Toggle between deg and rad for angle input
- Inverse Functions: Press
2ndbutton, then trig function for arcsin, arccos, arctan - Execute immediately when pressed
- Example:
sin(30°)= 0.5 in degree mode
-
Logarithmic Functions:
- lg (log): Common logarithm (base 10)
- ln: Natural logarithm (base e)
- Error handling for non-positive values
- Execute immediately when pressed
- Example:
lg(100)= 2,ln(e)= 1
-
Factorial (x!):
- Calculates factorial of non-negative integers
- Example:
5!= 120 - Limited to n ≤ 170 to prevent overflow
- Executes immediately when pressed
-
Reciprocal (1/x):
- Calculates the multiplicative inverse
- Example:
1/4= 0.25 - Error handling for division by zero
- Executes immediately when pressed
-
Square Root (√):
- Calculate square root with error handling for negative numbers
- Executes immediately when pressed
- Example:
√25= 5
-
Power (x^y):
- Raise any number to any power
- Uses standard expression building (requires equals)
- Example:
2 ^ 8 =→ 256
-
Percentage (%):
- Converts a number to its decimal percentage (divides by 100)
- Executes immediately when pressed
- Example:
50%= 0.5
-
Mathematical Constants:
- π (Pi): 3.141592653589793
- e (Euler's number): 2.718281828459045
- Insert when screen is clear or after an operator
-
Parentheses:
- Support for grouping operations in complex expressions
- Build nested expressions for precise control
- Example:
(5 + 3) × 2= 16
- Memory Recall (MR): Display the stored memory value (available in current layout)
- Full Memory Operations: M+, M-, MC available in code but simplified in current UI
- Memory is cleared when page refreshes
- Automatic Tracking: All calculations are saved automatically
- Timestamp: Each calculation includes the time it was performed
- Click to Recall: Click any history item to use its result
- Export to CSV: Download your calculation history
- Persistent Storage: History is maintained during your session
- Capacity: Stores up to 50 most recent calculations
- Light/Dark Modes: Toggle between two carefully designed color schemes
- Persistent Preference: Your theme choice is saved using localStorage
- Smooth Transitions: Theme changes animate smoothly
- Responsive Colors: All UI elements adapt to the selected theme
- Decimal Point: Only one decimal point is allowed per number
- Leading Zeros: Automatically handled (typing
0then5displays5) - Division by Zero: Protected with error message
- Operator Replacement: Pressing a different operator replaces the last one
- Screen Reset Logic: Smart handling of when to clear vs append to display
- Expression Validation: Invalid expressions are caught and handled gracefully
- Current Operand Display: Large, bold display of the number/expression you're entering
- Expression Display: Shows the complete mathematical expression being built above current input
- Number Formatting: Numbers formatted with commas for readability, but parentheses and expressions shown as-is
- Floating Point Precision: Handles floating-point arithmetic with proper rounding (10 decimal places)
- Real-time Feedback: See your expression update as you type
- Error Messages: Clear error notifications for invalid operations
-
Dual Mode Calculator:
-
Scientific Mode (5-column grid):
- Row 1: 2nd | deg | sin | cos | tan
- Row 2: x^y | lg | ln | ( | )
- Row 3: √ | AC | DEL | % | ÷
- Row 4: x! | 7 | 8 | 9 | ×
- Row 5: 1/x | 4 | 5 | 6 | −
- Row 6: π | 1 | 2 | 3 | +
- Row 7: ⇄ | e | 0 | . | =
-
Standard Mode (4-column grid):
- Row 1: AC | DEL | % | ÷
- Row 2: 7 | 8 | 9 | ×
- Row 3: 4 | 5 | 6 | −
- Row 4: 1 | 2 | 3 | +
- Row 5: ⇄ | 0 | . | =
-
-
Mode Toggle: Orange ⇄ button in bottom left to switch between modes
-
Responsive Layout: Works on various screen sizes (desktop, tablet, mobile)
-
Color-Coded Buttons:
- Gray: Numbers (0-9, decimal point)
- Orange: Arithmetic operators (÷, ×, −, +), Delete, and Mode toggle
- Blue: Functions (square root, percentage)
- Purple: Scientific functions (trig, log, factorial, reciprocal, constants, parentheses)
- Green: Equals button
- Red: All Clear (AC)
-
2nd Function Toggle: Purple button that lights up when activated for inverse functions
-
Degree/Radian Mode: Toggle button that switches between 'deg' and 'rad'
-
Theme Toggle: Moon/sun icon in header for easy theme switching
-
Visual Feedback: Buttons animate on hover and click
-
Gradient Background: Modern gradient backdrop that changes with theme
-
Dark/Light Display: High-contrast display for easy reading in both themes
-
History Panel: Clean, scrollable list of past calculations with export options
The calculator has been tested for:
-
Basic Operations
- ✓ Addition, Subtraction, Multiplication, Division
- ✓ BODMAS order of operations
- ✓ Chained operations (e.g.,
5 + 3 × 2) - ✓ Operator replacement
-
Expression Building
- ✓ Real-time expression display
- ✓ Complex expressions with multiple operators
- ✓ Parentheses support
- ✓ Expression persistence until equals pressed
-
Advanced Operations
- ✓ Square root (√)
- ✓ Percentage (%)
- ✓ Power (x^y)
- ✓ Factorial (x!)
- ✓ Reciprocal (1/x)
-
Trigonometric Functions
- ✓ Sine, Cosine, Tangent
- ✓ Inverse functions (arcsin, arccos, arctan)
- ✓ Degree/Radian mode toggle
- ✓ Immediate execution
-
Logarithmic Functions
- ✓ Common logarithm (lg)
- ✓ Natural logarithm (ln)
- ✓ Error handling for invalid inputs
-
Mathematical Constants
- ✓ Pi (π) insertion
- ✓ Euler's number (e) insertion
-
User Interface
- ✓ Mode toggle (Scientific ⇄ Standard)
- ✓ 5-column scientific layout
- ✓ 4-column standard layout
- ✓ Button event handling (no double-click issues)
- ✓ Theme toggle (light/dark)
-
History Features
- ✓ Calculation logging with timestamps
- ✓ History recall
- ✓ CSV export
- ✓ History clearing
-
Edge Cases
- ✓ Division by zero
- ✓ Square root of negative numbers
- ✓ Logarithm of non-positive numbers
- ✓ Factorial of negative/non-integer/large numbers
- ✓ Multiple decimal points
- ✓ Very large numbers
- ✓ Very small decimals
- ✓ Negative results
- ✓ Empty expressions
- ✓ Expressions ending with operators
-
Input Methods
- ✓ Mouse clicks
- ✓ Keyboard input with correct symbols (×, ÷, −)
- ✓ Touch input (mobile devices)
- ✓ Delete/Backspace after calculations
- Maximum precision is limited to 10 decimal places to avoid floating-point errors
- Very large numbers (beyond JavaScript's safe integer range) may lose precision
- History is limited to 50 most recent calculations
- History is not persistent across browser sessions (clears on page refresh)
- Memory is cleared on page refresh
- Scientific notation display is now supported for very large/small numbers
- Parentheses are auto-balanced before evaluation
- Complex nested expressions may require careful input
All previously planned features have been implemented, plus major improvements:
- ✓ BODMAS/PEMDAS Support - Correctly evaluates expressions following mathematical order of operations
- ✓ Expression Building System - Build complete mathematical expressions with real-time display
- ✓ Operator Replacement - Change operators before entering next number
- ✓ Unary Operation Handling - Immediate execution for functions like √, sin, log, etc.
- ✓ Binary Operation Chaining - Properly chain multiple operations like
5 + 3 × 2 - ✓ Smart Screen Reset - Intelligent handling of when to clear vs append to display
- ✓ Standalone Function Evaluation - Evaluate expressions like
sin(π/2)without requiring operations - ✓ Proper Function Wrapping - Degree/radian conversion properly wrapped for accurate results
- ✓ Nested Function Support - Support for nested functions like
sin(cos(30))
- ✓ Trigonometric Functions - sin, cos, tan with degree/radian modes
- ✓ Inverse Trigonometric Functions - arcsin, arccos, arctan accessible via 2nd button
- ✓ 2nd Button Toggle - Stays active until manually toggled off, disables deg/rad when active
- ✓ Button Label Updates - Trig buttons change to sin⁻¹, cos⁻¹, tan⁻¹ when 2nd is active
- ✓ Logarithmic Functions - Common (lg) and natural (ln) logarithms
- ✓ Function Notation Display - Shows functions as sin(, cos(, arcsin(, etc. for clarity
- ✓ Factorial Function - x! for non-negative integers
- ✓ Reciprocal Function - 1/x operation
- ✓ Mathematical Constants - π and e buttons with symbol display
- ✓ Pi Symbol Display - Displays π symbol instead of numeric value for cleaner expressions
- ✓ Constant Recognition - Recognizes both π symbol and "pi" text, "e" for Euler's number
- ✓ Parentheses Support - ( and ) visible in expressions
- ✓ Automatic Multiplication -
4(3)automatically interprets as4×(3) - ✓ Power Operation - x^y for exponentiation
- ✓ Dual-Mode Calculator - Toggle between basic (5-column) and Basic (4-column) modes
- ✓ Expression Display - See your complete expression as you build it
- ✓ Fixed Button Layout - Properly arranged 5-column basic grid
- ✓ No Double-Click Issues - Fixed event listener duplication
- ✓ Keyboard Support - Proper operator symbols (×, ÷, −) in keyboard shortcuts
- ✓ Enhanced Button Colors - Improved color scheme for better visual distinction:
- Orange: Operators (+, −, ×, ÷)
- Red/Pink: Clear (AC) and Delete (DEL)
- Purple: Scientific functions (sin, cos, tan, log, ln, etc.)
- Pink (active): 2nd button when toggled
- Cyan: General functions (√, %, x², etc.)
- Green: Equals (=)
- ✓ Mode Toggle Constraints - 2nd button disabled in radian mode, deg/rad disabled when 2nd active
- ✓ Memory functions (M+, M-, MR, MC) - Available in code
- ✓ Percentage calculations - Simple conversion (÷ 100)
- ✓ Square root and power operations - Full support
- ✓ Calculation history - Complete history panel with timestamps and CSV export
- ✓ Theme customization - Light/dark mode with localStorage persistence
- ✓ Delete function - Works correctly after calculations
Potential features for future versions:
- Auto-balancing parentheses
- Expression syntax highlighting
- Step-by-step solution display
- Hyperbolic trigonometric functions (sinh, cosh, tanh)
- More advanced memory operations in UI
- Graph plotting for functions
- Unit conversion capabilities
- Customizable color themes (beyond light/dark)
- Complex number support
- Equation solver
- Matrix operations
- Persistent calculation history across sessions
The calculator works in all modern browsers that support:
- ES6 JavaScript features
- CSS Grid and Flexbox
- CSS Custom Properties
- Modern event handling
Recommended browsers:
- Chrome/Edge 60+
- Firefox 60+
- Safari 12+
- Opera 47+