Welcome to the String Calculator project!
-
String Calculator Functionality:
- Handles comma-separated numbers and sums them up.
- Supports newlines as delimiters.
- Allows custom delimiters specified in the format:
//[delimiter]\n[numbers...]
. - Throws an error for negative numbers and lists all negative values in the error message.
- Returns
0
for empty strings.
-
Frontend Interface:
- Input box for entering numbers or custom delimiter strings.
- Dynamic display of calculation results or error messages.
- Clear and Calculate buttons for user interaction.
- Responsive design using Tailwind CSS.
-
Modularity and Scalability:
- Clean separation of concerns between logic (
add
function) and UI (StringCalculator
component). - Easily extendable for additional functionality or styling.
- Clean separation of concerns between logic (
-
Deployed Application:
- Live demo hosted here.
- Frontend: React with TypeScript
- Styling: Tailwind CSS
- Build Tool: Vite
- Testing: Vitest
- Deployment: Vercel/Netlify (link provided above)
The add
function handles the following cases:
- Empty String: Returns
0
. - Single Number: Returns the number itself.
- Multiple Numbers: Returns their sum.
- Newline Delimiters: Handles strings with numbers separated by commas or newlines (e.g.,
"1\n2,3"
). - Custom Delimiters: Parses custom delimiters specified in the format
//[delimiter]\n[numbers...]
(e.g.,"//;\n1;2"
). - Negative Numbers: Throws an exception with a message listing all negative numbers.
- Input field for entering the string.
- Buttons:
Calculate
: Triggers theadd
function and displays the result.Clear
: Resets the input and result fields.
- Dynamic error and result handling:
- Displays the sum for valid input.
- Displays an error message for invalid input or negative numbers.
- Clone the Repository:
git clone https://github.com/iRajatDas/string-calculator.git cd string-calculator
- Install Dependencies:
pnpm install
- Run the Development Server:
pnpm dev
The application should now be running on http://localhost:3000.
The project uses Vitest for testing. To run the tests, use the following command:
pnpm test