diff --git a/Sprint-3/3-stretch/credit-card-validator.js b/Sprint-3/3-stretch/credit-card-validator.js new file mode 100644 index 000000000..16f2af214 --- /dev/null +++ b/Sprint-3/3-stretch/credit-card-validator.js @@ -0,0 +1,46 @@ +function validateCreditCard(cardNumber) { + const numStr = cardNumber.toString(); + + if (numStr.length !== 16) { + return false; + } // Number must be 16 digits. + + const uniqueDigits = new Set(numStr); + if (uniqueDigits.size < 2) { + return false; + } // Must have at least two different digits. + + const lastDigit = Number(numStr[numStr.length - 1]); + if (lastDigit % 2 !== 0) { + return false; + } // Last digit must be even. + + let sum = 0; + for (let char of numStr) { + sum += Number(char); + } // Sum of all digits must be greater than 16. + + if (sum <= 16) { + return false; + } // Sum check + + return true; +} + +// Test cases: + +// Number must be 16 digits +console.log(validateCreditCard(9999)); // false +console.log(validateCreditCard(9999888877776662)); // true + +// Must have at least two different digits +console.log(validateCreditCard(1111111111111111)); // false +console.log(validateCreditCard(1234567890123456)); // true + +// Last digit must be even +console.log(validateCreditCard(1234567890123451)); // false +console.log(validateCreditCard(1234567890123452)); // true + +// Sum of all digits must be greater than 16 +console.log(validateCreditCard(1111111111111110)); // false +console.log(validateCreditCard(1234567890123456)); // true diff --git a/Sprint-3/3-stretch/find.js b/Sprint-3/3-stretch/find.js index c7e79a2f2..61d7ae449 100644 --- a/Sprint-3/3-stretch/find.js +++ b/Sprint-3/3-stretch/find.js @@ -20,6 +20,13 @@ console.log(find("code your future", "z")); // Pay particular attention to the following: // a) How the index variable updates during the call to find +// It keeps increasing by 1 until it finds the character or reaches the end of the string(the length of the string). + // b) What is the if statement used to check +// The if statement checks if the current character in the string is equal to the target character. + // c) Why is index++ being used? +// The index++ is used to move to the next character in the string during each iteration of the loop. + // d) What is the condition index < str.length used for? +// The condition index < str.length is used to ensure that the loop continues as long as there are characters left in the string to check. diff --git a/Sprint-3/3-stretch/password-validator.js b/Sprint-3/3-stretch/password-validator.js index b55d527db..c375d6d31 100644 --- a/Sprint-3/3-stretch/password-validator.js +++ b/Sprint-3/3-stretch/password-validator.js @@ -1,6 +1,39 @@ +// List of previously used passwords +const passwords = ["Abc!1", "Hello#2"]; + function passwordValidator(password) { - return password.length < 5 ? false : true -} + // 1. Must have at least 5 characters + if (password.length < 5) { + return false; + } + + // 2. Must have at least one uppercase letter (A-Z) + if (!/[A-Z]/.test(password)) { + return false; + } + + // 3. Must have at least one lowercase letter (a-z) + if (!/[a-z]/.test(password)) { + return false; + } + // 4. Must have at least one number (0-9) + if (!/[0-9]/.test(password)) { + return false; + } + + // 5. Must have at least one symbol from the allowed list + if (!/[!#$%.*&]/.test(password)) { + return false; + } + + // 6. Must not be a previously used password + if (passwords.includes(password)) { + return false; + } + + // ✅ If all rules pass → valid + return true; +} -module.exports = passwordValidator; \ No newline at end of file +module.exports = passwordValidator; diff --git a/Sprint-3/3-stretch/password-validator.test.js b/Sprint-3/3-stretch/password-validator.test.js index 8fa3089d6..13efd204b 100644 --- a/Sprint-3/3-stretch/password-validator.test.js +++ b/Sprint-3/3-stretch/password-validator.test.js @@ -16,11 +16,59 @@ You must breakdown this problem in order to solve it. Find one test case first a */ 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 + // Arrange + const password = "12345"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +// 2. Password is too short +test("password must be at least 5 characters", () => { + const password = "Ab1!"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +// 3. Password must contain at least one uppercase letter +test("password must contain at least one uppercase letter", () => { + const password = "abc!1"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +// 4. Password must contain at least one lowercase letter +test("password must contain at least one lowercase letter", () => { + const password = "ABC!1"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +// 5. Password must contain at least one number +test("password must contain at least one number", () => { + const password = "Abc!@"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +// 6. Password must contain at least one symbol (! # $ % . * &) +test("password must contain at least one symbol", () => { + const password = "Abc12"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +// 7. Password must not be a previously used password +test("password must not be in the previous passwords array", () => { + const password = "Abc!1"; // this is in the passwords array inside the validator + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +// 8. Password is valid when all requirements are met +test("password is valid when it meets all requirements", () => { + const password = "Abc!2"; + const result = isValidPassword(password); + expect(result).toEqual(true); +});