Finished
88 * @returns {number}
99 */
1010export const prime = (ordinal) => {
11 throw new Error('Remove this line and implement the function');
11 // Edge Case: Invalid input - zero
12 if (ordinal === 0) {
13 throw new Error('there is no zeroth prime');
14 }
15
16 // Edge Case: Invalid input - negative number
17 if (ordinal < 0) {
18 throw new Error('prime number index must be positive');
19 }
20
21 // Edge Case: Invalid input - non-integer
22 if (!Number.isInteger(ordinal)) {
23 throw new Error('prime number index must be an integer');
24 }
25
26 // Edge Case: First prime number
27 if (ordinal === 1) {
28 return 2;
29 }
30
31 let count = 1; // We already know 2 is prime
32 let candidate = 1; // Start checking from 3 (first odd number after 2)
33
34 while (count < ordinal) {
35 candidate += 2; // Only check odd numbers
36 if (isPrime(candidate)) {
37 count++;
38 }
39 }
40
41 return candidate;
1242};
43
44/**
45 * Helper function to check if a number is prime
46 * @param {number} num
47 * @returns {boolean}
48 */
49function isPrime(num) {
50 // Edge Case: Numbers less than 2 are not prime
51 if (num < 2) {
52 return false;
53 }
54
55 // Edge Case: 2 is prime
56 if (num === 2) {
57 return true;
58 }
59
60 // Edge Case: Even numbers greater than 2 are not prime
61 if (num % 2 === 0) {
62 return false;
63 }
64
65 // Check odd divisors up to square root of num
66 const sqrt = Math.sqrt(num);
67 for (let i = 3; i <= sqrt; i += 2) {
68 if (num % i === 0) {
69 return false;
70 }
71 }
72
73 return true;
74}
75
76// Handled Edge Cases: ordinal is 0, ordinal is negative, ordinal is non-integer, ordinal is 1, num is less than 2, num is 2, num is even and greater than 2
Test NameStatus
first prime
Pass
second prime
Pass
sixth prime
Pass
big prime
Pass
there is no zeroth prime
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.