Skip to content

Commit 70f9400

Browse files
committed
added a test for only 16 digits with spaces or operators, updated variable name, and refactored previous password as a function input
1 parent 791b52a commit 70f9400

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

Sprint-3/3-stretch/creditCard-validator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
function creditCardValidator(cardNumber) {
22
// declare the length of valid card number
33
const VALID_LENGTH = 16;
4+
const minimumSum = 16;
45

56
// Remove all - and spaces from the input
67
const sanitized = cardNumber.replace(/[-\s]/g, "");
@@ -30,7 +31,7 @@ function creditCardValidator(cardNumber) {
3031
const sum = sanitized
3132
.split("")
3233
.reduce((total, digit) => total + Number(digit), 0);
33-
if (sum <= VALID_LENGTH) {
34+
if (sum <= minimumSum) {
3435
return false;
3536
}
3637

Sprint-3/3-stretch/creditCard-validator.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ describe("creditCardValidator", () => {
3838
expect(creditCardValidator("12345678901234567")).toBe(false);
3939
expect(creditCardValidator("1234-5678-9012-34567")).toBe(false);
4040
});
41+
42+
// test for only 16 digits without spaces or dashes
43+
test("should return true for a card number with 16 digits without spaces or dashes", () => {
44+
expect(creditCardValidator("1234567890123456")).toBe(true);
45+
expect(creditCardValidator("0000000000000000")).toBe(false);
46+
expect(creditCardValidator("1111111111111111")).toBe(false);
47+
});
4148
});

Sprint-3/3-stretch/password-validator.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
function passwordValidator(password) {
1+
const previousPasswords = [
2+
"123Ab!",
3+
"abcD1#",
4+
"Password1!",
5+
"Qwerty1*",
6+
"Zxcvbnm2$",
7+
];
8+
9+
function isValidPassword(password, previousPasswords) {
210
const lengthCondition = password.length >= 5;
311
console.log(`length condition: ${lengthCondition}`);
412

@@ -14,13 +22,6 @@ function passwordValidator(password) {
1422
const symbolCondition = /[!#$%.*&]/.test(password);
1523
console.log(`symbol condition: ${symbolCondition}`);
1624

17-
const previousPasswords = [
18-
"123Ab!",
19-
"abcD1#",
20-
"Password1!",
21-
"Qwerty1*",
22-
"Zxcvbnm2$",
23-
];
2425
const notInPreviousPasswords = !previousPasswords.includes(password);
2526
console.log(`previous password condition: ${notInPreviousPasswords}`);
2627

@@ -34,4 +35,4 @@ function passwordValidator(password) {
3435
);
3536
}
3637

37-
module.exports = passwordValidator;
38+
module.exports = { isValidPassword, previousPasswords };

Sprint-3/3-stretch/password-validator.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,58 @@ To be valid, a password must:
1414
1515
You must breakdown this problem in order to solve it. Find one test case first and get that working
1616
*/
17-
const isValidPassword = require("./password-validator");
17+
const { isValidPassword, previousPasswords } = require("./password-validator");
1818

1919
describe("passwordValidator", () => {
2020
const validPassword = "123Ab*";
2121

2222
test("returns true for passwords with at least 5 characters", () => {
23-
expect(isValidPassword(validPassword)).toBe(true);
23+
expect(isValidPassword(validPassword, previousPasswords)).toBe(true);
2424
});
2525

2626
test("return true for passwords with at least one uppercase letter", () => {
27-
expect(isValidPassword(validPassword)).toBe(true);
27+
expect(isValidPassword(validPassword, previousPasswords)).toBe(true);
2828
});
2929

3030
test("return true for passwords with at least one lowercase letter", () => {
31-
expect(isValidPassword(validPassword)).toBe(true);
31+
expect(isValidPassword(validPassword, previousPasswords)).toBe(true);
3232
});
3333

3434
test("return true for passwords with at least one number", () => {
35-
expect(isValidPassword(validPassword)).toBe(true);
35+
expect(isValidPassword(validPassword, previousPasswords)).toBe(true);
3636
});
3737

3838
test("return true for passwords with at least one non-alphanumeric symbol", () => {
39-
expect(isValidPassword(validPassword)).toBe(true);
39+
expect(isValidPassword(validPassword, previousPasswords)).toBe(true);
4040
});
4141

4242
test("return true for passwords that are not in the previous passwords array", () => {
43-
expect(isValidPassword(validPassword)).toBe(true);
43+
expect(isValidPassword(validPassword, previousPasswords)).toBe(true);
4444
});
4545

4646
// tests for false cases
4747

4848
test("returns false for passwords with less than 5 characters", () => {
49-
expect(isValidPassword("1234")).toBe(false);
49+
expect(isValidPassword("1234", previousPasswords)).toBe(false);
5050
});
5151

5252
test("returns false for passwords without an uppercase letter", () => {
53-
expect(isValidPassword("123ab*")).toBe(false);
53+
expect(isValidPassword("123ab*", previousPasswords)).toBe(false);
5454
});
5555

5656
test("returns false for passwords without a lowercase letter", () => {
57-
expect(isValidPassword("123AB*")).toBe(false);
57+
expect(isValidPassword("123AB*", previousPasswords)).toBe(false);
5858
});
5959

6060
test("returns false for passwords without a number", () => {
61-
expect(isValidPassword("abAB*")).toBe(false);
61+
expect(isValidPassword("abAB*", previousPasswords)).toBe(false);
6262
});
6363

6464
test("returns false for passwords without a non-alphanumeric symbol", () => {
65-
expect(isValidPassword("123Abc")).toBe(false);
65+
expect(isValidPassword("123Abc", previousPasswords)).toBe(false);
6666
});
6767

6868
test("returns false for passwords that are in the previous passwords array", () => {
69-
expect(isValidPassword("123Ab!")).toBe(false);
69+
expect(isValidPassword("123Ab!", previousPasswords)).toBe(false);
7070
});
7171
});

0 commit comments

Comments
 (0)