diff --git a/Sprint-3/3-stretch/find.js b/Sprint-3/3-stretch/find.js index c7e79a2f2..a93a43d75 100644 --- a/Sprint-3/3-stretch/find.js +++ b/Sprint-3/3-stretch/find.js @@ -20,6 +20,21 @@ console.log(find("code your future", "z")); // Pay particular attention to the following: // a) How the index variable updates during the call to find + +// Starts at 0, increments by 1 each iteration (index++) +// Moves through each character position: 0 → 1 → 2 → 3 → etc. + // b) What is the if statement used to check + +// Checks if the character at the current position (str[index]) matches the character we're searching for (char) + // c) Why is index++ being used? + +// To move to the next character position in the string for the next iteration +// Without it, we'd have an infinite loop stuck at the same position + // d) What is the condition index < str.length used for? + +// Ensures we don't go beyond the string length +// Prevents accessing invalid memory locations +// Stops the loop when we've checked all characters \ 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..5aad5bf60 100644 --- a/Sprint-3/3-stretch/password-validator.js +++ b/Sprint-3/3-stretch/password-validator.js @@ -1,6 +1,31 @@ function passwordValidator(password) { - return password.length < 5 ? false : true -} + // Check length (at least 5 characters) + if (password.length < 5) { + return false; + } + + // Check for at least one uppercase letter (A-Z) + if (!/[A-Z]/.test(password)) { + return false; + } + + // Check for at least one lowercase letter (a-z) + if (!/[a-z]/.test(password)) { + return false; + } + // Check for at least one number (0-9) + if (!/[0-9]/.test(password)) { + return false; + } + + // Check for at least one special symbol: !, #, $, . + if (!/[!#$.]/.test(password)) { + return false; + } + + // If all checks pass, return true + return true; +} module.exports = passwordValidator; \ 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..b308483da 100644 --- a/Sprint-3/3-stretch/password-validator.test.js +++ b/Sprint-3/3-stretch/password-validator.test.js @@ -14,13 +14,38 @@ 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 + // Arrange + const password = "Abc1!"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +}); + +test("password with less than 5 characters returns false", () => { + expect(isValidPassword("Ab1!")).toEqual(false); +}); + +test("password without uppercase returns false", () => { + expect(isValidPassword("abc1!")).toEqual(false); +}); + +test("password without lowercase returns false", () => { + expect(isValidPassword("ABC1!")).toEqual(false); +}); + +test("password without number returns false", () => { + expect(isValidPassword("Abcd!")).toEqual(false); +}); + +test("password without special symbol returns false", () => { + expect(isValidPassword("Abcd1")).toEqual(false); +}); + +test("valid password with all requirements returns true", () => { + expect(isValidPassword("Abc1!")).toEqual(true); +}); \ No newline at end of file