Skip to content

Commit 3b30fc0

Browse files
committed
fix: report divide-by-zero on mod block instead of Not a Number
1 parent d09973a commit 3b30fc0

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

js/blocks/NumberBlocks.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ function setupNumberBlocks(activity) {
5656
*/
5757
const handleMathError = (logo, error, blk, onError = NANERRORMSG) => {
5858
logo.stopTurtle = true;
59+
if (error && error.message === "DivByZeroError") {
60+
activity.errorMsg(ZERODIVIDEERRORMSG, blk);
61+
return;
62+
}
5963
activity.errorMsg(onError, blk);
6064
};
6165

js/utils/__tests__/mathutils.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe("MathUtility", () => {
130130
});
131131

132132
test("throws error for invalid inputs", () => {
133-
expect(() => MathUtility.doMod("a", 3)).toThrow("Invalid number input");
133+
expect(() => MathUtility.doMod("a", 3)).toThrow("NanError");
134134
});
135135

136136
// Edge case tests
@@ -159,7 +159,7 @@ describe("MathUtility", () => {
159159
});
160160

161161
test("throws error when second arg is string", () => {
162-
expect(() => MathUtility.doMod(10, "a")).toThrow("Invalid number input");
162+
expect(() => MathUtility.doMod(10, "a")).toThrow("NanError");
163163
});
164164
});
165165

@@ -556,7 +556,7 @@ describe("MathUtility", () => {
556556

557557
describe("edge cases - Infinity, NaN, and boundary values", () => {
558558
test("doMod throws an error when divisor is zero", () => {
559-
expect(() => MathUtility.doMod(5, 0)).toThrow();
559+
expect(() => MathUtility.doMod(5, 0)).toThrow("DivByZeroError");
560560
});
561561

562562
test("doSqrt handles Infinity", () => {

js/utils/mathutils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ class MathUtility {
130130
static doMod(a, b) {
131131
if (typeof a === "number" && typeof b === "number") {
132132
if (Number(b) === 0) {
133-
throw new Error("Division by zero");
133+
throw new Error("DivByZeroError");
134134
}
135135
return Number(a) % Number(b);
136136
} else {
137-
throw new Error("Invalid number input");
137+
throw new Error("NanError");
138138
}
139139
}
140140

0 commit comments

Comments
 (0)