File tree Expand file tree Collapse file tree 1 file changed +5
-2
lines changed
Expand file tree Collapse file tree 1 file changed +5
-2
lines changed Original file line number Diff line number Diff line change 11function cardValidator ( cardNumber )
22{
3+ //check the card length must be 16 digit if the card number is less than 16 this condition will throw error
34 if ( cardNumber . length !== 16 )
45 {
56 throw new Error ( "card number length must be 16" ) ;
67 }
78 let sum = 0 ;
9+ // Loop through each digit to check if it is a valid number
810 for ( let i = 0 ; i < 15 ; i ++ )
9- {
11+ { // if character is not between "0" to "9", it's not a digit.
1012 if ( cardNumber [ i ] < "0" || cardNumber [ i ] > "9" )
1113 {
1214 throw new Error ( "not a valid number" ) ;
1315 }
14-
16+ //convert the digit to number and add it to sum total.
1517 sum = sum + Number ( cardNumber [ i ] ) ;
1618 }
1719 for ( let i = 1 ; i < cardNumber . length ; i ++ )
@@ -21,6 +23,7 @@ function cardValidator(cardNumber)
2123 throw new Error ( " should have at least two different digits represented" ) ;
2224 }
2325 }
26+ // This condition will check the sum of all digit must be greater than 16 if its less than or Equal to 16 this condition will throw error
2427 if ( sum <= 16 )
2528 {
2629 throw new Error ( "sum of all number less than 16" ) ;
You can’t perform that action at this time.
0 commit comments