Skip to content

Commit 4bb8fb0

Browse files
committed
Update function to check for decimal digits.
1 parent 9bb4e9f commit 4bb8fb0

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

‎sprint-3-stretch-exercises/credit-card-validator.js‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export function creditCardValidator(cardNumber) {
2-
// rejects the number if itn's exactly 16 characters or contain non-digits
3-
if (cardNumber.length !== 16 || !Number.isInteger(Number(cardNumber))) {
2+
// rejects the number if itn's exactly 16 characters
3+
if (cardNumber.length !== 16) {
44
return false;
55
}
6-
6+
// the last digit must be even, so reject it if dividing by 2 leaves a reminder
77
if (cardNumber.slice(-1) % 2 !== 0) {
88
return false;
99
}
@@ -12,7 +12,10 @@ export function creditCardValidator(cardNumber) {
1212
let digitSum = 0;
1313

1414
for (const digit of cardNumber) {
15-
// the last digit must be even, so reject it if dividing by 2 leaves a reminder
15+
// every character must be a digit from 0 to 9
16+
if (digit < "0" || digit > "9") {
17+
return false;
18+
}
1619
digitSum += Number(digit);
1720

1821
// if any digit differs from the first one, at least two different digits are used
@@ -21,7 +24,7 @@ export function creditCardValidator(cardNumber) {
2124
}
2225
}
2326

24-
// the number needs two different digits and a digit sum greater than 16
27+
// needs two different digits and a digit sum greater than 16
2528
if (!hasDifferentDigit || digitSum <= 16) {
2629
return false;
2730
}

0 commit comments

Comments
 (0)