An open API service indexing awesome lists of open source software.

https://github.com/ayokanmi-adejola/javascript-calculator

A beautiful JavaScript calculator built with React & TypeScript. Implements formula logic with proper order of operations, handles all edge cases, and passes all FreeCodeCamp tests. Features chain calculations, decimal precision, consecutive operator handling, and beautiful gradient UI.
https://github.com/ayokanmi-adejola/javascript-calculator

mobile-first-workflow react18 tailwindcss typescript vite

Last synced: 3 months ago
JSON representation

A beautiful JavaScript calculator built with React & TypeScript. Implements formula logic with proper order of operations, handles all edge cases, and passes all FreeCodeCamp tests. Features chain calculations, decimal precision, consecutive operator handling, and beautiful gradient UI.

Awesome Lists containing this project

README

          

# JavaScript Calculator - FreeCodeCamp Project

A beautiful, fully-featured JavaScript calculator built with React and TypeScript that meets all FreeCodeCamp requirements. This production-ready calculator implements proper formula logic with order of operations and handles all edge cases.

![image](https://github.com/user-attachments/assets/ac35a5b5-491f-4835-9f23-9ad8a6aeb583)

## ๐Ÿš€ Features

### Core Functionality
- โœ… Complete calculator with all basic operations (+, -, ร—, รท)
- โœ… Formula logic with proper order of operations (not immediate execution)
- โœ… Chain calculations with operator precedence
- โœ… Decimal point support with precision handling
- โœ… Clear functionality that resets to initial state
- โœ… Error handling for invalid operations

### Advanced Features
- โœ… Consecutive operator handling (keeps last operator, allows negative numbers)
- โœ… Prevents multiple leading zeros
- โœ… Operator continuation after equals for new calculations
- โœ… Precise decimal calculations with proper rounding (10 decimal places)
- โœ… Formula display showing calculation history
- โœ… Responsive design for all screen sizes

### Design Elements
- ๐ŸŽจ Modern gradient design with elegant color scheme
- ๐ŸŽฏ Clean, readable typography with proper button hierarchy
- โœจ Smooth hover animations and visual feedback
- ๐Ÿ“ฑ Responsive grid layout
- ๐ŸŽช Professional spacing using 8px system
- ๐ŸŒŸ Subtle shadows and rounded corners for depth

## ๐Ÿ› ๏ธ Technology Stack

- **React 18** - Modern React with hooks
- **TypeScript** - Type-safe development
- **Tailwind CSS** - Utility-first CSS framework
- **Vite** - Fast build tool and dev server

## ๐Ÿ“‹ FreeCodeCamp Requirements

This calculator fulfills all 15 user stories from the FreeCodeCamp JavaScript Calculator project:

1. โœ… Contains clickable element with `id="equals"`
2. โœ… Contains 10 clickable number elements (0-9) with proper IDs
3. โœ… Contains 4 operator elements with IDs: `add`, `subtract`, `multiply`, `divide`
4. โœ… Contains decimal element with `id="decimal"`
5. โœ… Contains clear element with `id="clear"`
6. โœ… Contains display element with `id="display"`
7. โœ… Clear button resets calculator to initial state (shows 0)
8. โœ… Numbers display as they're input
9. โœ… Chain calculations work correctly with proper order of operations
10. โœ… Prevents numbers from beginning with multiple zeros
11. โœ… Decimal handling (only one decimal per number)
12. โœ… Operations work on decimal numbers
13. โœ… Consecutive operators handled properly (last operator wins, except negative)
14. โœ… Operators after equals start new calculation with previous result
15. โœ… Decimal precision maintained (handles calculations like 2/7 properly)

## ๐ŸŽฏ Calculator Logic

This calculator implements **Formula Logic** with proper order of operations, not immediate execution logic. This means:

- `3 + 5 ร— 6 - 2 รท 4 = 32.5` (Formula Logic)
- Operations follow mathematical precedence (multiplication/division before addition/subtraction)

## ๐Ÿš€ Getting Started

### Prerequisites
- Node.js (version 16 or higher)
- npm or yarn

### Installation

1. Clone the repository:
```bash
git clone
cd javascript-calculator
```

2. Install dependencies:
```bash
npm install
```

3. Start the development server:
```bash
npm run dev
```

4. Open your browser and navigate to `http://localhost:5173`

### Building for Production

```bash
npm run build
```

The built files will be in the `dist` directory.

## ๐Ÿงช Testing

This project includes the FreeCodeCamp test suite. The tests will automatically run when you open the calculator in your browser.

## ๐Ÿ“ Project Structure

```
src/
โ”œโ”€โ”€ App.tsx # Main app component
โ”œโ”€โ”€ Calculator.tsx # Calculator component with all logic
โ”œโ”€โ”€ main.tsx # React app entry point
โ”œโ”€โ”€ index.css # Tailwind CSS imports
โ””โ”€โ”€ vite-env.d.ts # Vite type definitions
```

## ๐ŸŽจ Design Philosophy

The calculator follows modern design principles:

- **Hierarchy**: Clear visual hierarchy using typography and spacing
- **Contrast**: High contrast for readability and accessibility
- **Balance**: Symmetrical layout with proper proportions
- **Movement**: Subtle animations that enhance user experience
- **Consistency**: Consistent spacing, colors, and interactions throughout

## ๐Ÿ”ง Key Implementation Details

### State Management
- Uses React hooks for state management
- Tracks display value, formula, previous key type, and input state
- Handles complex state transitions for proper calculator behavior

### Expression Evaluation
- Safe evaluation using Function constructor with strict mode
- Proper operator precedence handling
- Floating-point precision management
- Error handling for invalid expressions

### Edge Case Handling
- Consecutive operators (5 + * 7 = 35)
- Negative numbers (5 * -5 = -25)
- Leading zeros prevention
- Decimal point validation
- Division by zero and other error states

## ๐Ÿ‘จโ€๐Ÿ’ป Author

**Ayokanmi Adejola**