diff --git a/Sprint-3/3-stretch/find.js b/Sprint-3/3-stretch/find.js index c7e79a2f2..f309177f5 100644 --- a/Sprint-3/3-stretch/find.js +++ b/Sprint-3/3-stretch/find.js @@ -19,7 +19,23 @@ console.log(find("code your future", "z")); // Use the Python Visualiser to help you play computer with this example and observe how this code is executed // Pay particular attention to the following: -// a) How the index variable updates during the call to find +// a) How the index variable updates during the call to find a char + + // As the code runs, index starts from zero and runs until the length of the string, + // It starts from zero and in each loop it gets incremented until a char is found within the string. + // b) What is the if statement used to check + + // It checks if the char is in the string. If it exists.. next step is to return the index of the char. + + // c) Why is index++ being used? + + // To check if the given char is present starting from 0 till str.length. + + // d) What is the condition index < str.length used for? + + // As long as index is less than the length of the string, keep looping until the char is found. + +// In find.js explanations given to the question. \ No newline at end of file diff --git a/Sprint-3/3-stretch/password-validator.js b/Sprint-3/3-stretch/password-validator.js index b55d527db..d3fa0e12e 100644 --- a/Sprint-3/3-stretch/password-validator.js +++ b/Sprint-3/3-stretch/password-validator.js @@ -1,6 +1,15 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true +function isValidPassword(password, previousPasswords = []) { + return ( + password.length >= 5 && + /[A-Z]/.test(password) && + /[a-z]/.test(password) && + /[0-9]/.test(password) && + /[!#$%.*&]/.test(password) && + !previousPasswords.includes(password) + ); } -module.exports = passwordValidator; \ No newline at end of file +module.exports = isValidPassword; + +// In password-validator.js function implemented. \ No newline at end of file diff --git a/Sprint-3/3-stretch/password-validator.test.js b/Sprint-3/3-stretch/password-validator.test.js index 8fa3089d6..829b592d2 100644 --- a/Sprint-3/3-stretch/password-validator.test.js +++ b/Sprint-3/3-stretch/password-validator.test.js @@ -15,12 +15,45 @@ 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"); +const previousPasswords = ["Mmd1!", "XyZ2$", "Tes5%"]; + + 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 + const password = "Ki55$"; + const result = isValidPassword(password, previousPasswords); // pass the array + expect(result).toEqual(true); +}); + +test("password has at least one uppercase", () => { + const password = "Uo85*"; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(true);npx +}); + +test("password has at least one lowercase", () => { + const password = "Qf#45"; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(true); +}); + +test("password has at least one number", () => { + const password = "Cz!35"; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(true); +}); + +test("password has at least one special symbol", () => { + const password = "Re*19"; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(true); +}); + +test("password must not be a previous password", () => { + const password = "Mmd1!"; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(false); + +}); + + +// In Password-validator.test.js cases tested and passed.