diff --git a/sprint-3-stretch-exercises/find.js b/sprint-3-stretch-exercises/find.js index c7e79a2f2..19c6d1a56 100644 --- a/sprint-3-stretch-exercises/find.js +++ b/sprint-3-stretch-exercises/find.js @@ -20,6 +20,10 @@ console.log(find("code your future", "z")); // Pay particular attention to the following: // a) How the index variable updates during the call to find +// - index starts at 0 and increase by 1 each time in the loop. When a character match is found the index return and the function stops. // b) What is the if statement used to check +// - It checks whether the character at position index of str is exactly equal to char. If so it returns that position. // c) Why is index++ being used? +// - index++ checks the next character of str. Without index would stay at 0 and the loop would never stop. // d) What is the condition index < str.length used for? +// - To check the number of characters in str. Once index equals str.length and every characters is checked the loop stops diff --git a/sprint-3-stretch-exercises/password-validator.js b/sprint-3-stretch-exercises/password-validator.js index ac70324a5..03a6cf79d 100644 --- a/sprint-3-stretch-exercises/password-validator.js +++ b/sprint-3-stretch-exercises/password-validator.js @@ -1,3 +1,16 @@ -export function isValidPassword(password) { - return password.length >= 5; +export function isValidPassword(password, previousPasswords = []) { + if (previousPasswords.includes(password)) { + return false; + } + if (password.length < 5) { + return false; + } + + for (const char of password) { + if (!/[A-Z]/.test(password)) return false; + if (!/[a-z]/.test(password)) return false; + if (!/[0-9]/.test(password)) return false; + if (!/[!#$%.*&]/.test(password)) return false; + } + return true; } diff --git a/sprint-3-stretch-exercises/password-validator.test.js b/sprint-3-stretch-exercises/password-validator.test.js index 2a58a0fb6..cc5ba7919 100644 --- a/sprint-3-stretch-exercises/password-validator.test.js +++ b/sprint-3-stretch-exercises/password-validator.test.js @@ -16,11 +16,53 @@ You must breakdown this problem in order to solve it. Find one test case first a */ import { isValidPassword } from "./password-validator.js"; 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 = "Az9!"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +// Have at least one English uppercase letter (A-Z) +test("password has at least uppercase letter", () => { + const password = "1az9!"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +// Have at least one English lowercase letter (a-z) +test("password has at least lowercase letter", () => { + const password = "1AZ9!"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +// Have at least one number (0-9) +test("password has at least one number (0-9)", () => { + const password = "Aazc!"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +// Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&") +test("password has at least non-alphanumeric symbols", () => { + const password = "1Az99"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +// Must not be any previous password in the passwords array. +test("password must not be any previous password in the passwords array", () => { + const password = "1Az9!"; + const previousPassword = ["1Az9!", "1HBz2!"]; + const result = isValidPassword(password, previousPassword); + expect(result).toEqual(false); +}); + +// Valid password returns true. +test("valid password returns true", () => { + const password = "1Az9!"; + const result = isValidPassword(password); + expect(result).toEqual(true); +});