diff --git a/sprint-3-stretch-exercises/credit-card-validator.js b/sprint-3-stretch-exercises/credit-card-validator.js new file mode 100644 index 000000000..73414a95d --- /dev/null +++ b/sprint-3-stretch-exercises/credit-card-validator.js @@ -0,0 +1,32 @@ +export function creditCardValidator(cardNumber) { + // rejects the number if itn's exactly 16 characters + if (cardNumber.length !== 16) { + return false; + } + // the last digit must be even, so reject it if dividing by 2 leaves a reminder + if (cardNumber.slice(-1) % 2 !== 0) { + return false; + } + + let hasDifferentDigit = false; + let digitSum = 0; + + for (const digit of cardNumber) { + // every character must be a digit from 0 to 9 + if (digit < "0" || digit > "9") { + return false; + } + digitSum += Number(digit); + + // if any digit differs from the first one, at least two different digits are used + if (digit !== cardNumber[0]) { + hasDifferentDigit = true; + } + } + + // needs two different digits and a digit sum greater than 16 + if (!hasDifferentDigit || digitSum <= 16) { + return false; + } + return true; +} diff --git a/sprint-3-stretch-exercises/credit-card-validator.test.js b/sprint-3-stretch-exercises/credit-card-validator.test.js new file mode 100644 index 000000000..ac851aa61 --- /dev/null +++ b/sprint-3-stretch-exercises/credit-card-validator.test.js @@ -0,0 +1,42 @@ +// Case 1 valid card numbers. +import { creditCardValidator } from "./credit-card-validator.js"; +test("should return true for a valid card number", () => { + const number = "2222222222222224"; + const result = creditCardValidator(number); + expect(result).toEqual(true); +}); + +// Case 2 number must be 16 digits. +test("should return false when the numbers has fewer than 16 digits", () => { + const number = "123"; + const result = creditCardValidator(number); + expect(result).toEqual(false); +}); + +// Case 3 numbers must be whole numbers. +test("should return false when the number contains a letter", () => { + const number = "22222222222222b4"; + const result = creditCardValidator(number); + expect(result).toEqual(false); +}); + +// Case 4 numbers must have at least two different digits represented. +test("should return false when all digits are the same.", () => { + const number = "2222222222222222"; + const result = creditCardValidator(number); + expect(result).toEqual(false); +}); + +// Case 5 the final digit of the number must be even. +test("should return false when the last digit is odd", () => { + const number = "2222222222222223"; + const result = creditCardValidator(number); + expect(result).toEqual(false); +}); + +// Case 6 the sum of all the digits must be greater than 16. +test("should return false when the sum of digits is 16 or less", () => { + const number = "1111111111110002"; + const result = creditCardValidator(number); + expect(result).toEqual(false); +}); diff --git a/sprint-3-stretch-exercises/find.js b/sprint-3-stretch-exercises/find.js index c7e79a2f2..e4c61c3f1 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++ increments by 1 each time the lope find a match. 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..e0e048381 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; + } + + 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..9b799f51c 100644 --- a/sprint-3-stretch-exercises/password-validator.test.js +++ b/sprint-3-stretch-exercises/password-validator.test.js @@ -16,11 +16,62 @@ 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 previousPassword1 = ["1Az9!", "1HBz2!"]; + const result1 = isValidPassword(password, previousPassword1); + expect(result1).toEqual(false); + + const previousPassword2 = ["1HBz2!", "1Az9!"]; + const result2 = isValidPassword(password, previousPassword2); + expect(result2).toEqual(false); + + const previousPassword3 = ["1Az8!", "1HBz2!"]; + const result3 = isValidPassword(password, previousPassword3); + expect(result3).toEqual(true); +}); + +// Valid password returns true. +test("valid password returns true", () => { + const password = "1Az9!"; + const result = isValidPassword(password); + expect(result).toEqual(true); +});