Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions js/blocks/NumberBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ function setupNumberBlocks(activity) {
*/
const handleMathError = (logo, error, blk, onError = NANERRORMSG) => {
logo.stopTurtle = true;
if (error && error.message === "DivByZeroError") {
Comment thread
rish106-hub marked this conversation as resolved.
activity.errorMsg(ZERODIVIDEERRORMSG, blk);
return;
}
activity.errorMsg(onError, blk);
};

Expand Down
6 changes: 3 additions & 3 deletions js/utils/__tests__/mathutils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe("MathUtility", () => {
});

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

// Edge case tests
Expand Down Expand Up @@ -159,7 +159,7 @@ describe("MathUtility", () => {
});

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

Expand Down Expand Up @@ -556,7 +556,7 @@ describe("MathUtility", () => {

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

test("doSqrt handles Infinity", () => {
Expand Down
4 changes: 2 additions & 2 deletions js/utils/mathutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ class MathUtility {
static doMod(a, b) {
if (typeof a === "number" && typeof b === "number") {
if (Number(b) === 0) {
throw new Error("Division by zero");
throw new Error("DivByZeroError");
}
return Number(a) % Number(b);
} else {
throw new Error("Invalid number input");
throw new Error("NanError");
}
}

Expand Down
Loading