Skip to content

Commit 925f6b0

Browse files
committed
Complete Sprint 3 stretch password and card validators
1 parent 3372770 commit 925f6b0

File tree

4 files changed

+149
-14
lines changed

4 files changed

+149
-14
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Validate whether a credit card number meets the rules from card-validator.md
2+
function validateCreditCardNumber(cardNumber) {
3+
// Rule 1: the value must be exactly 16 characters long and contain only digits.
4+
if (!/^\d{16}$/.test(cardNumber)) {
5+
return false;
6+
}
7+
8+
// Rule 2: not all digits can be the same.
9+
const uniqueDigits = new Set(cardNumber);
10+
if (uniqueDigits.size < 2) {
11+
return false;
12+
}
13+
14+
// Rule 3: the final digit must be even.
15+
const lastDigit = Number(cardNumber[cardNumber.length - 1]);
16+
if (lastDigit % 2 !== 0) {
17+
return false;
18+
}
19+
20+
// Rule 4: the sum of all digits must be greater than 16.
21+
let sum = 0;
22+
for (const digit of cardNumber) {
23+
sum += Number(digit);
24+
}
25+
26+
if (sum <= 16) {
27+
return false;
28+
}
29+
30+
return true;
31+
}
32+
33+
module.exports = validateCreditCardNumber;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const validateCreditCardNumber = require("./card-validator");
2+
3+
test("returns true for a valid card number", () => {
4+
expect(validateCreditCardNumber("9999777788880000")).toEqual(true);
5+
expect(validateCreditCardNumber("6666666666661666")).toEqual(true);
6+
});
7+
8+
test("returns false when the card contains non-digit characters", () => {
9+
expect(validateCreditCardNumber("a92332119c011112")).toEqual(false);
10+
});
11+
12+
test("returns false when all digits are the same", () => {
13+
expect(validateCreditCardNumber("4444444444444444")).toEqual(false);
14+
});
15+
16+
test("returns false when the sum of digits is not greater than 16", () => {
17+
expect(validateCreditCardNumber("1111111111111110")).toEqual(false);
18+
});
19+
20+
test("returns false when the final digit is odd", () => {
21+
expect(validateCreditCardNumber("6666666666666661")).toEqual(false);
22+
});
23+
24+
test("returns false when the number is not 16 digits long", () => {
25+
expect(validateCreditCardNumber("1234")).toEqual(false);
26+
});
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
1+
// A small list of previous passwords that must not be reused.
2+
const previousPasswords = ["Password1!", "Welcome2#", "Strong3$"];
3+
14
function passwordValidator(password) {
2-
return password.length < 5 ? false : true
3-
}
5+
// Password must be at least 5 characters long.
6+
if (password.length < 5) {
7+
return false;
8+
}
9+
10+
// Password must contain at least one uppercase letter.
11+
if (!/[A-Z]/.test(password)) {
12+
return false;
13+
}
14+
15+
// Password must contain at least one lowercase letter.
16+
if (!/[a-z]/.test(password)) {
17+
return false;
18+
}
419

20+
// Password must contain at least one number.
21+
if (!/[0-9]/.test(password)) {
22+
return false;
23+
}
24+
25+
// Password must contain at least one allowed symbol.
26+
if (!/[!#$%.*&]/.test(password)) {
27+
return false;
28+
}
29+
30+
// Password must not match any previous password.
31+
if (previousPasswords.includes(password)) {
32+
return false;
33+
}
34+
35+
return true;
36+
}
537

6-
module.exports = passwordValidator;
38+
module.exports = passwordValidator;
Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
Password Validation
33
44
Write a program that should check if a password is valid
@@ -10,17 +10,61 @@ To be valid, a password must:
1010
- Have at least one English lowercase letter (a-z)
1111
- Have at least one number (0-9)
1212
- Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&")
13-
- Must not be any previous password in the passwords array.
13+
- Must not be any previous password in the passwords array.
1414
1515
You must breakdown this problem in order to solve it. Find one test case first and get that working
1616
*/
1717
const isValidPassword = require("./password-validator");
18-
test("password has at least 5 characters", () => {
19-
// Arrange
20-
const password = "12345";
21-
// Act
22-
const result = isValidPassword(password);
23-
// Assert
24-
expect(result).toEqual(true);
25-
}
26-
);
18+
19+
test("password is valid when it meets all rules", () => {
20+
// Arrange
21+
const password = "Ab1!c";
22+
23+
// Act
24+
const result = isValidPassword(password);
25+
26+
// Assert
27+
expect(result).toEqual(true);
28+
});
29+
30+
test("password is invalid when it is shorter than 5 characters", () => {
31+
const password = "A1!a";
32+
const result = isValidPassword(password);
33+
34+
expect(result).toEqual(false);
35+
});
36+
37+
test("password is invalid when it has no uppercase letter", () => {
38+
const password = "abcde1!";
39+
const result = isValidPassword(password);
40+
41+
expect(result).toEqual(false);
42+
});
43+
44+
test("password is invalid when it has no lowercase letter", () => {
45+
const password = "ABCDE1!";
46+
const result = isValidPassword(password);
47+
48+
expect(result).toEqual(false);
49+
});
50+
51+
test("password is invalid when it has no number", () => {
52+
const password = "Abcde!";
53+
const result = isValidPassword(password);
54+
55+
expect(result).toEqual(false);
56+
});
57+
58+
test("password is invalid when it has no required symbol", () => {
59+
const password = "Abcde1";
60+
const result = isValidPassword(password);
61+
62+
expect(result).toEqual(false);
63+
});
64+
65+
test("password is invalid when it has been used before", () => {
66+
const password = "Password1!";
67+
const result = isValidPassword(password);
68+
69+
expect(result).toEqual(false);
70+
});

0 commit comments

Comments
 (0)