Skip to content

Commit e3cda72

Browse files
Implement cardValidator with validation rules and passing tests
1 parent 3fb4767 commit e3cda72

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function cardValidator(cardNumber)
2+
{
3+
if(cardNumber.length !==16)
4+
{
5+
throw new Error("card number length must be 16");
6+
}
7+
let sum = 0;
8+
for(let i = 0; i<15; i++)
9+
{
10+
if(cardNumber[i]<"0" || cardNumber[i]>"9")
11+
{
12+
throw new Error("not a valid number");
13+
}
14+
15+
sum = sum + Number(cardNumber[i]);
16+
}
17+
for(let i = 1; i< cardNumber.length;i++)
18+
{
19+
if(cardNumber[0] == cardNumber[i])
20+
{
21+
throw new Error(" should have at least two different digits represented");
22+
}
23+
}
24+
if(sum <= 16)
25+
{
26+
throw new Error("sum of all number less than 16");
27+
}
28+
else if(cardNumber.slice(-1)%2 !== 0 )
29+
{
30+
throw new Error("last digit not an even number");
31+
}
32+
else
33+
{
34+
return true;
35+
}
36+
}
37+
module.exports = cardValidator;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
## **PROJECT: Credit Card Validator**
22

33
In this project you'll write a script that validates whether or not a credit card number is valid.
4-
4+
You must have at least two different digits represented (all of the digits cannot be the same).
55
Here are the rules for a valid number:
66

77
- Number must be 16 digits, all of them must be numbers.
8-
- You must have at least two different digits represented (all of the digits cannot be the same).
8+
-
99
- The final digit must be even.
1010
- The sum of all the digits must be greater than 16.
1111

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const cardValidator = require("./card-validator");
2+
3+
test("Should card number length must be 16", ()=>{
4+
const cardNumber = "546313000814253";
5+
expect(()=> cardValidator(cardNumber)).toThrow();
6+
});
7+
test("should all of them must be Number", ()=>{
8+
const cardNumber = "546313000814253A";
9+
expect(()=> cardValidator(cardNumber)).toThrow();
10+
});
11+
test(" should have at least two different digits represented", ()=>{
12+
const cardNumber = "1111111111111111";
13+
expect(()=> cardValidator(cardNumber)).toThrow();
14+
})
15+
test("should The sum of all the digits must be greater than 16", ()=>{
16+
const cardNumber = "1111111111111111";
17+
expect(()=> cardValidator(cardNumber)).toThrow();
18+
});
19+
test("should the final digit must be even", ()=>{
20+
const cardNumber = "5463130008141535";
21+
expect(()=> cardValidator(cardNumber)).toThrow();
22+
});
23+
test("Should a valid card Number", ()=>{
24+
const cardNumber = "5463130076112436";
25+
expect(cardValidator(cardNumber)).toEqual(true);
26+
});

0 commit comments

Comments
 (0)