Skip to content

fix: handle division by zero in divide function#130

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

fix: handle division by zero in divide function#130
quantcode-agent[bot] wants to merge 1 commit intomainfrom
quantcode/job-job-51bb5a5f

Conversation

@quantcode-agent
Copy link
Copy Markdown

Summary

  • Fixed the division-by-zero bug in src/calculator.ts by adding a guard clause that throws Error("Division by zero") when the divisor is 0
  • Added three tests in test/calculator.test.ts covering normal division and the zero-divisor error case

Changes

src/calculator.ts

Added input validation to divide:

export function divide(a: number, b: number): number {
  if (b === 0) {
    throw new Error("Division by zero")
  }
  return a / b
}

test/calculator.test.ts

  • Imported divide alongside existing imports
  • Added tests: divide(10, 2) === 5, divide(9, 3) === 3, and divide(5, 0) throws "Division by zero"

All 6 tests pass.

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