Finished
1{
12//
23// This is only a SKELETON file for the 'Luhn' exercise. It's been provided as a
34// convenience to get you started writing code faster.
78 * @param {string} number
89 * @returns {boolean}
910 */
10export const valid = (number) => {
11 throw new Error('Remove this line and implement the function');
11const valid = (number) => {
12 // Edge Case: Strings of length 1 or less are not valid
13 if (number.length <= 1) {
14 return false;
15 }
16
17 // Remove all spaces from the string
18 const cleanedNumber = number.replace(/\s/g, '');
19
20 // Edge Case: Strings with only spaces (after cleaning become empty)
21 if (cleanedNumber.length === 0) {
22 return false;
23 }
24
25 // Edge Case: Strings with non-digit characters are disallowed
26 if (!/^\d+$/.test(cleanedNumber)) {
27 return false;
28 }
29
30 let sum = 0;
31 const length = cleanedNumber.length;
32
33 // Process digits from right to left
34 for (let i = length - 1; i >= 0; i--) {
35 let digit = parseInt(cleanedNumber[i], 10);
36
37 // Double every second digit starting from the right
38 if ((length - 1 - i) % 2 === 1) {
39 digit *= 2;
40 // If doubling results in a number > 9, subtract 9
41 if (digit > 9) {
42 digit -= 9;
43 }
44 }
45
46 sum += digit;
47 }
48
49 // The number is valid if the sum is divisible by 10
50 return sum % 10 === 0;
1251};
52
53// Handled Edge Cases: Strings of length 1 or less, strings with only spaces, strings with non-digit characters
54
55module.exports = { valid };
56}
Test NameStatus
single zero with space is invalid
Fail
single digit strings can not be valid
Pass
a single zero is invalid
Pass
a simple valid SIN that remains valid if reversed
Pass
a simple valid SIN that becomes invalid if reversed
Pass
a valid Canadian SIN
Pass
invalid Canadian SIN
Pass
invalid credit card
Pass
invalid long number with an even remainder
Pass
invalid long number with a remainder divisible by 5
Pass
valid number with an even number of digits
Pass
valid number with an odd number of spaces
Pass
valid strings with a non-digit added at the end invalid
Pass
valid strings with punctuation included become invalid
Pass
valid strings with symbols included become invalid
Pass
more than a single zero is valid
Pass
input digit 9 is correctly converted to output digit 9
Pass
very long input is valid
Pass
valid luhn with an odd number of digits and non zero first digit
Pass
using ascii value for non-doubled non-digit isn't allowed
Pass
using ascii value for doubled non-digit isn't allowed
Pass
non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed
Pass

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