Skip to content

fix: throw error on division by zero in calculator#143

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

fix: throw error on division by zero in calculator#143
quantcode-agent[bot] wants to merge 1 commit intomainfrom
quantcode/job-job-68a8c73e

Conversation

@quantcode-agent
Copy link
Copy Markdown

Summary

  • Fixes the division-by-zero bug in src/calculator.ts by adding a guard clause that throws an Error("Division by zero") when the divisor is 0
  • Adds test coverage for both normal division and the division-by-zero error case

Changes

  • src/calculator.ts: Added if (b === 0) throw new Error("Division by zero") at the top of the divide function
  • test/calculator.test.ts: Added test cases verifying divide(10, 2) === 5 and divide(10, 0) throws

Assumptions

  • Throwing an Error is the appropriate behavior (vs returning null/NaN/Infinity)
  • The error message "Division by zero" is sufficient for consumers of this function

All 5 tests pass.

- Add guard clause in divide() to throw 'Division by zero' when divisor is 0
- Add test cases for normal division and division-by-zero error
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