Skip to content

Commit f0d7b15

Browse files
committed
adding a variable for card valid length
1 parent 9c8e884 commit f0d7b15

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Sprint-3/3-stretch/creditCard-validator.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
function creditCardValidator(cardNumber) {
2+
// declare the length of valid card number
3+
const VALID_LENGTH = 16;
4+
25
// Remove all - and spaces from the input
36
const sanitized = cardNumber.replace(/[-\s]/g, "");
47

58
//check if the length of the sanitized input is 16
6-
if (sanitized.length !== 16) {
9+
if (sanitized.length !== VALID_LENGTH) {
710
return false;
811
}
912
// check if the sanitized input contains only digits
10-
if (!/^\d{16}$/.test(sanitized)) {
13+
if (!new RegExp(`^\\d{${VALID_LENGTH}}$`).test(sanitized)) {
1114
return false;
1215
}
1316

@@ -27,7 +30,7 @@ function creditCardValidator(cardNumber) {
2730
const sum = sanitized
2831
.split("")
2932
.reduce((total, digit) => total + Number(digit), 0);
30-
if (sum <= 16) {
33+
if (sum <= VALID_LENGTH) {
3134
return false;
3235
}
3336

0 commit comments

Comments
 (0)