diff --git a/Sprint-3/4-stretch/card-validator.js b/Sprint-3/4-stretch/card-validator.js new file mode 100644 index 000000000..6f3e5f6c2 --- /dev/null +++ b/Sprint-3/4-stretch/card-validator.js @@ -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; diff --git a/Sprint-3/4-stretch/card-validator.test.js b/Sprint-3/4-stretch/card-validator.test.js new file mode 100644 index 000000000..1c0e5043b --- /dev/null +++ b/Sprint-3/4-stretch/card-validator.test.js @@ -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); +}); diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index b55d527db..ab688c084 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -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) { + 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; \ No newline at end of file +module.exports = passwordValidator; diff --git a/Sprint-3/4-stretch/password-validator.test.js b/Sprint-3/4-stretch/password-validator.test.js index 8fa3089d6..b47e4f5f4 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -1,4 +1,4 @@ -/* +/* Password Validation Write a program that should check if a password is valid @@ -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); -} -); \ No newline at end of file + +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); +});