Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions src/functions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// functions.js
export function primeFactorization(input) {
if (input === null || input === undefined || input === "") {
return "No prime factors";
return "Please enter a number";
}

// Accept number | string | bigint
Expand Down Expand Up @@ -55,10 +55,13 @@ export function primeFactorization(input) {
.join(" * ");
}

// Generate all primes up to a given limit using the Sieve of Eratosthenes
export function generatePrimes(limit) {
// Same as before; limit is a small Number (e.g., 1000)
const n = Math.floor(Number(limit));
if (n < 2 || !Number.isFinite(n)) return [];
if (n < 2 || !Number.isFinite(n)) {
return [];
}
const sieve = Array(n + 1).fill(true);
sieve[0] = sieve[1] = false;
for (let i = 2; i * i <= n; i++) {
Expand Down
17 changes: 17 additions & 0 deletions tests/Functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ describe("primeFactorization", () => {
expect(primeFactorization(0)).toBe("No prime factors");
expect(primeFactorization(-5)).toBe("No prime factors");
});

it("should handle BigInt input", () => {
expect(primeFactorization(12345678901234567890n)).toBe(
"2 * 3<sup>2</sup> * 5 * 101 * 3541 * 3607 * 3803 * 27961",
);
});

it("should handle string input", () => {
expect(primeFactorization("56")).toBe("2<sup>3</sup> * 7");
expect(primeFactorization("97")).toBe("97");
expect(primeFactorization("not a number")).toBe("No prime factors");
});
});

describe("generatePrimes", () => {
Expand All @@ -48,4 +60,9 @@ describe("generatePrimes", () => {
expect(generatePrimes(0)).toEqual([]);
expect(generatePrimes(-10)).toEqual([]);
});

it("should handle non-integer limits", () => {
expect(generatePrimes(10.7)).toEqual([2, 3, 5, 7]);
expect(generatePrimes(15.2)).toEqual([2, 3, 5, 7, 11, 13]);
});
});
10 changes: 8 additions & 2 deletions tests/test.setup.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
import { randomUUID } from "node:crypto"; // Node.js built-in crypto
global.crypto = { randomUUID }; // Mock or polyfill necessary crypto functions
import { randomUUID } from "node:crypto";

if (!globalThis.crypto?.randomUUID) {
Object.defineProperty(globalThis, "crypto", {
value: { randomUUID },
configurable: true,
});
}