Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Sprint-3/4-stretch/card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Validate whether a credit card number meets the rules from card-validator.md
function validateCreditCardNumber(cardNumber) {
// Rule 1: the value must be exactly 16 characters long and contain only digits.
if (!/^\d{16}$/.test(cardNumber)) {
return false;
}

// Rule 2: not all digits can be the same.
const uniqueDigits = new Set(cardNumber);
if (uniqueDigits.size < 2) {
return false;
}

// Rule 3: the final digit must be even.
const lastDigit = Number(cardNumber[cardNumber.length - 1]);
if (lastDigit % 2 !== 0) {
return false;
}

// Rule 4: the sum of all digits must be greater than 16.
let sum = 0;
for (const digit of cardNumber) {
sum += Number(digit);
}

if (sum <= 16) {
return false;
}

return true;
}

module.exports = validateCreditCardNumber;
26 changes: 26 additions & 0 deletions Sprint-3/4-stretch/card-validator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const validateCreditCardNumber = require("./card-validator");

test("returns true for a valid card number", () => {
expect(validateCreditCardNumber("9999777788880000")).toEqual(true);
expect(validateCreditCardNumber("6666666666661666")).toEqual(true);
});

test("returns false when the card contains non-digit characters", () => {
expect(validateCreditCardNumber("a92332119c011112")).toEqual(false);
});

test("returns false when all digits are the same", () => {
expect(validateCreditCardNumber("4444444444444444")).toEqual(false);
});

test("returns false when the sum of digits is not greater than 16", () => {
expect(validateCreditCardNumber("1111111111111110")).toEqual(false);
});

test("returns false when the final digit is odd", () => {
expect(validateCreditCardNumber("6666666666666661")).toEqual(false);
});

test("returns false when the number is not 16 digits long", () => {
expect(validateCreditCardNumber("1234")).toEqual(false);
});
43 changes: 40 additions & 3 deletions Sprint-3/4-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
// A list of previously used passwords that cannot be reused.
const previousPasswords = ["Password1!", "Welcome2#", "Strong3$"];

function passwordValidator(password) {
return password.length < 5 ? false : true
}
// Ensure a password value is provided and that it is a string.
if (typeof password !== "string") {
return false;
}

// Password must be at least 5 characters long.
if (password.length < 5) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could password just not be provided at all?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ykamal,

Good point, thank you.

I updated the function to handle the case where a password is not provided.
Previously the function accessed password.length, which would throw an error if the value was undefined.

I added an input validation check at the start of the function to ensure the password exists and is a string. If it is missing or not a string, the function now safely returns false.

I also added test cases to cover these scenarios.

return false;
}

// Password must contain at least one uppercase letter.
if (!/[A-Z]/.test(password)) {
return false;
}

// Password must contain at least one lowercase letter.
if (!/[a-z]/.test(password)) {
return false;
}

// Password must contain at least one number.
if (!/[0-9]/.test(password)) {
return false;
}

// Password must contain at least one allowed symbol.
if (!/[!#$%.*&]/.test(password)) {
return false;
}

// Password must not match any previous password.
if (previousPasswords.includes(password)) {
return false;
}

return true;
}

module.exports = passwordValidator;
module.exports = passwordValidator;
78 changes: 67 additions & 11 deletions Sprint-3/4-stretch/password-validator.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Password Validation

Write a program that should check if a password is valid
Expand All @@ -10,17 +10,73 @@ To be valid, a password must:
- Have at least one English lowercase letter (a-z)
- Have at least one number (0-9)
- Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&")
- Must not be any previous password in the passwords array.
- Must not be any previous password in the passwords array.

You must breakdown this problem in order to solve it. Find one test case first and get that working
*/
const isValidPassword = require("./password-validator");
test("password has at least 5 characters", () => {
// Arrange
const password = "12345";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
}
);

test("password is valid when it meets all rules", () => {
// Arrange
const password = "Ab1!c";

// Act
const result = isValidPassword(password);

// Assert
expect(result).toEqual(true);
});

test("password is invalid when it is shorter than 5 characters", () => {
const password = "A1!a";
const result = isValidPassword(password);

expect(result).toEqual(false);
});

test("password is invalid when it has no uppercase letter", () => {
const password = "abcde1!";
const result = isValidPassword(password);

expect(result).toEqual(false);
});

test("password is invalid when it has no lowercase letter", () => {
const password = "ABCDE1!";
const result = isValidPassword(password);

expect(result).toEqual(false);
});

test("password is invalid when it has no number", () => {
const password = "Abcde!";
const result = isValidPassword(password);

expect(result).toEqual(false);
});

test("password is invalid when it has no required symbol", () => {
const password = "Abcde1";
const result = isValidPassword(password);

expect(result).toEqual(false);
});

test("password is invalid when it has been used before", () => {
const password = "Password1!";
const result = isValidPassword(password);

expect(result).toEqual(false);
});

test("password is invalid when no password is provided", () => {
const result = isValidPassword();

expect(result).toEqual(false);
});

test("password is invalid when the value is not a string", () => {
const result = isValidPassword(12345);

expect(result).toEqual(false);
});
Loading