Skip to content

Commit b691c83

Browse files
Add comment to explain my code
1 parent e3cda72 commit b691c83

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Sprint-3/4-stretch/card-validator.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
function 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");

0 commit comments

Comments
 (0)