File tree Expand file tree Collapse file tree 1 file changed +6
-3
lines changed
Expand file tree Collapse file tree 1 file changed +6
-3
lines changed Original file line number Diff line number Diff line change 11function 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
You can’t perform that action at this time.
0 commit comments