Skip to content

fix: handle division-by-zero in calculator#132

Open
quantcode-agent[bot] wants to merge 1 commit intomainfrom
quantcode/job-job-a1e034df
Open

fix: handle division-by-zero in calculator#132
quantcode-agent[bot] wants to merge 1 commit intomainfrom
quantcode/job-job-a1e034df

Conversation

@quantcode-agent
Copy link
Copy Markdown

Summary

  • Fixed division-by-zero bug in src/calculator.ts by adding a guard that throws "Cannot divide by zero" when the divisor is 0
  • Added test cases in test/calculator.test.ts to verify both normal division and the error case

Changes

  • src/calculator.ts: Added if (b === 0) throw new Error("Cannot divide by zero") guard in the divide function
  • test/calculator.test.ts: Added test for successful division and test that division by zero throws the expected error

Testing

All 5 tests pass (bun test).

Assumptions

  • Throwing an error is the appropriate behaviour for division by zero (rather than returning Infinity or NaN)
  • The error message "Cannot divide by zero" follows the project's error messaging conventions

- Add guard in divide() to throw 'Cannot divide by zero' when divisor is 0
- Add test cases for normal division and division-by-zero error handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants