Skip to content

Commit dbeb4e3

Browse files
author
QuantCode Agent
committed
fix: handle division by zero in divide() and add tests
1 parent 18a7ef6 commit dbeb4e3

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/calculator.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ export function multiply(a: number, b: number): number {
1515
return a * b
1616
}
1717

18-
// BUG: Division by zero is not handled
18+
/**
19+
* Divides `a` by `b`.
20+
* @throws {Error} When `b` is zero.
21+
*/
1922
export function divide(a: number, b: number): number {
23+
if (b === 0) throw new Error("Division by zero")
2024
return a / b
2125
}

test/calculator.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { describe, test, expect } from "bun:test"
2-
import { add, subtract, multiply } from "../src/calculator"
3-
4-
// Note: divide is intentionally not tested — has a known bug
2+
import { add, subtract, multiply, divide } from "../src/calculator"
53

64
describe("calculator", () => {
75
test("adds two numbers", () => {
@@ -15,4 +13,14 @@ describe("calculator", () => {
1513
test("multiplies two numbers", () => {
1614
expect(multiply(4, 3)).toBe(12)
1715
})
16+
17+
describe("divide", () => {
18+
test("divides two numbers", () => {
19+
expect(divide(10, 2)).toBe(5)
20+
})
21+
22+
test("throws on division by zero", () => {
23+
expect(() => divide(5, 0)).toThrow("Division by zero")
24+
})
25+
})
1826
})

0 commit comments

Comments
 (0)