diff --git a/Sprint-3/4-stretch/card-validator.js b/Sprint-3/4-stretch/card-validator.js new file mode 100644 index 000000000..330e0223b --- /dev/null +++ b/Sprint-3/4-stretch/card-validator.js @@ -0,0 +1,23 @@ +function creditCardValidator(cardNum) { + // Splits the cardNum into an array of characters + const cardNumArray = cardNum.split(""); + + // Checks if length is 16, otherwise return false + if (cardNumArray.length !== 16) return false; + + // checks if all digits are numbers + if (!cardNumArray.every((num) => num >= 0 && num <= 9)) return false; + + // Checks if there are at least two different digits + const count = new Set(cardNumArray); + if (count.size < 2) return false; + + // Checks if the sum of all digits is greater than 16 + const sumOfDigits = cardNumArray.reduce((acc, curr) => acc + Number(curr), 0); + if (sumOfDigits <= 16) return false; + + //checks if last digit is even + if (cardNumArray[cardNumArray.length - 1] % 2 !== 0) return false; + return true; +} +console.log(creditCardValidator("1111111111111111")); diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index b55d527db..068423344 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -1,6 +1,11 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true -} +function passwordValidator(password, previousPasswords = []) { + if (password.length < 5) return false; + if (previousPasswords.includes(password)) return false; + previousPasswords.push(password); + + const rules = [/[A-Z]/, /[a-z]/, /[0-9]/, /[!#$%.*&]/]; + return rules.every((rule) => rule.test(password)); +} -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..c1f52aa7d 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -15,12 +15,65 @@ To be valid, a password must: 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("should return false if password has fewer than 5 characters", () => { + // Arrange + const password = "A1b&"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("should return false if password was previously used", () => { + // Arrange + const password = "5B43n21!"; + // Act + const result = isValidPassword(password, "5B43n21!"); + // Assert + expect(result).toEqual(false); +}); + +test("should return false if password does not contain an uppercase English letter", () => { + // Arrange + const password = "1a2345&"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("should return false if password does not contain an lowercase English letter", () => { + // Arrange + const password = "1B2345%"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("should return false if password has no numbers(0-9)", () => { + // Arrange + const password = "se!rjJN%Gk"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test('should return false if password has no special characters including "!", "#", "$", "%", ".", "*", "&"', () => { + // Arrange + const password = "sdkerjJNG23k"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("should return true if password meets all validation rules", () => { + // Arrange + const password = "sdkerj!JNG23k&"; + // Act + const result = isValidPassword(password, ["se!rjJN%G6k", "1B2h345%a"]); + // Assert + expect(result).toEqual(true); +});