diff --git a/sprint-3-stretch-exercises/card-validator.js b/sprint-3-stretch-exercises/card-validator.js new file mode 100644 index 000000000..0126a9979 --- /dev/null +++ b/sprint-3-stretch-exercises/card-validator.js @@ -0,0 +1,39 @@ +function isValidCreditCardNumber(cardNumber){ + if(cardNumber.length !== 16) { + return false; + } + + const allowedCharacters = "0123456789"; + for(let i = 0; i < cardNumber.length; i++) { + const character = cardNumber[i]; + if(!allowedCharacters.includes(character)) { + return false; + } + } + + const firstDigit = cardNumber[0]; + let foundDifferentDigit = false; + for(let i = 0; i < cardNumber.length; i++) { + if(cardNumber[i] !== firstDigit) { + foundDifferentDigit = true; + } + } + if(foundDifferentDigit === false){ + return false; + } + + const lastDigit = Number(cardNumber[15]); + if(lastDigit % 2 !== 0) { + return false; + } + + let sum = 0; + for(let i = 0; i < cardNumber.length; i++){ + sum += Number(cardNumber[i]); + } + if(sum <= 16){ + return false; + } + + return true; +} \ No newline at end of file diff --git a/sprint-3-stretch-exercises/find.js b/sprint-3-stretch-exercises/find.js index c7e79a2f2..f04859dd2 100644 --- a/sprint-3-stretch-exercises/find.js +++ b/sprint-3-stretch-exercises/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 +// "Index" starts at 0 and increases by 1 when there is no match. + // b) What is the if statement used to check +// It's checking whether the character at the current position equals the character we are looking for. + // c) Why is index++ being used? +// Without index++ there would be an infinite loop, whereby index would never change. + // d) What is the condition index < str.length used for? +// It is used to stop the loop as soon as it reaches the end of the string, so we don't look at positions that don't exist. diff --git a/sprint-3-stretch-exercises/password-validator.js b/sprint-3-stretch-exercises/password-validator.js index ac70324a5..c8cd9164e 100644 --- a/sprint-3-stretch-exercises/password-validator.js +++ b/sprint-3-stretch-exercises/password-validator.js @@ -1,3 +1,14 @@ -export function isValidPassword(password) { - return password.length >= 5; +export function isValidPassword(password, previousPassword = []) { + 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; + if (previousPassword.includes(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..4d3b8589f 100644 --- a/sprint-3-stretch-exercises/password-validator.test.js +++ b/sprint-3-stretch-exercises/password-validator.test.js @@ -15,12 +15,43 @@ 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 */ import { isValidPassword } from "./password-validator.js"; + test("password has at least 5 characters", () => { // Arrange - const password = "12345"; + const password = "Ab7#"; // Act const result = isValidPassword(password); // Assert - expect(result).toEqual(true); -} -); \ No newline at end of file + expect(result).toEqual(false); +}); + +test("password has at least one uppercase letter", () => { + const password = "ab7#x"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +test("password has at least one lowercase letter", () => { + const password = "AB7#X"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +test("password has at least one number", () => { + const password = "Abc#x"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +test("password has at least one non-alphanumeric symbol", () => { + const password = "Ab7nx"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +test("password must not be a previous password", () => { + const password = "Ab7#x"; + const previousPassword = ["Ab7#x"]; + const result = isValidPassword(password, previousPassword); + expect(result).toEqual(false); +}); \ No newline at end of file