diff --git a/sprint-3-stretch-exercises/card-validator.js b/sprint-3-stretch-exercises/card-validator.js new file mode 100644 index 000000000..70ad21b0c --- /dev/null +++ b/sprint-3-stretch-exercises/card-validator.js @@ -0,0 +1,24 @@ +export function isCardValid(cardNum) { + if (typeof cardNum !== "number") { + return false; + } + const has16Length = String(cardNum).length === 16; + const seenDigits = []; + const isFinalEven = cardNum % 2 === 0; + let total = 0; + for (const num of String(cardNum)) { + if (!seenDigits.includes(num)) { + seenDigits.push(num); + } + const regex = /^\d$/; + if (!regex.test(num)) { + return false; + } + total += Number(num); + } + if (has16Length && isFinalEven && total > 16 && seenDigits.length >= 2) { + return true; + } else { + return false; + } +} diff --git a/sprint-3-stretch-exercises/card-validator.test.js b/sprint-3-stretch-exercises/card-validator.test.js new file mode 100644 index 000000000..c9f75a713 --- /dev/null +++ b/sprint-3-stretch-exercises/card-validator.test.js @@ -0,0 +1,59 @@ +// - Number must be 16 digits, all of them must be numbers. +// - You must have at least two different digits represented (all of the digits cannot be the same). +// - The final digit must be even. +// - The sum of all the digits must be greater than 16. + +import { isCardValid } from "./card-validator.js"; + +test("card number should have 16 digits", () => { + const cardNumber = 1234567890987654; + const result = isCardValid(cardNumber); + expect(result).toEqual(true); +}); + +test("card number should have at least two different digits, all of them cannot be the same", () => { + const cardNumber = 1111111111111111; + const result = isCardValid(cardNumber); + expect(result).toEqual(false); +}); + +test("final card digit must be even", () => { + const cardNumber = 1111111111111118; + const result = isCardValid(cardNumber); + expect(result).toEqual(true); +}); + +test("sum of all the digits must be greater than 16", () => { + const cardNumber = 2222333344445554; + const result = isCardValid(cardNumber); + expect(result).toEqual(true); +}); + +test("cardNumber must contains only numbers", () => { + const cardNumber = "1233341234444444"; + const result = isCardValid(cardNumber); + expect(result).toEqual(false); +}); + +test("cardNumber sum is less than 16, should return false", () => { + const cardNumber = 1111111111111110; + const result = isCardValid(cardNumber); + expect(result).toEqual(false); +}); + +test("cardNumber uses invalid characters,should return false", () => { + const cardNumber = "a92332119c011112"; + const result = isCardValid(cardNumber); + expect(result).toEqual(false); +}); +test("cardNumber uses only one type of number,should return false", () => { + const cardNumber = 4444444444444444; + const result = isCardValid(cardNumber); + expect(result).toEqual(false); +}); + +test("cardNumber has odo final number, should return false", () => { + const cardNumber = 6666666666666661; + const result = isCardValid(cardNumber); + expect(result).toEqual(false); +}); diff --git a/sprint-3-stretch-exercises/find.js b/sprint-3-stretch-exercises/find.js index c7e79a2f2..566d4d043 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 updates from value of 0 to increment value of +1 every time it loop through while loop if it reach length of str and won't find what it looks for function return -1 // b) What is the if statement used to check +//the if statement checks if str index character is at the same position as char index position we're looking for // c) Why is index++ being used? +//every time our function loops through while statement index position increment by 1 // d) What is the condition index < str.length used for? +//while index is smaller than length of str the loop would run, if it reaches same value function return -1 instead diff --git a/sprint-3-stretch-exercises/password-validator.js b/sprint-3-stretch-exercises/password-validator.js index ac70324a5..1b6917400 100644 --- a/sprint-3-stretch-exercises/password-validator.js +++ b/sprint-3-stretch-exercises/password-validator.js @@ -1,3 +1,32 @@ +const passwords = []; export function isValidPassword(password) { - return password.length >= 5; + let hasUpper = false; + let hasLower = false; + let hasNumbers = false; + let hasSpecialChars = false; + const minLength = password.length >= 5; + const upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const lowerLetters = "abcdefghijklmnopqrstuvwxyz"; + const numbers = "0123456789"; + const specialChars = "!#$%.*&"; + + for (const char of password) { + hasUpper = upperLetters.includes(char) ? true : hasUpper; + hasLower = lowerLetters.includes(char) ? true : hasLower; + hasNumbers = numbers.includes(char) ? true : hasNumbers; + hasSpecialChars = specialChars.includes(char) ? true : hasSpecialChars; + } + if ( + minLength && + hasLower && + hasUpper && + hasNumbers && + hasSpecialChars && + !passwords.includes(password) + ) { + passwords.push(password); + return true; + } else { + return false; + } } diff --git a/sprint-3-stretch-exercises/password-validator.test.js b/sprint-3-stretch-exercises/password-validator.test.js index 2a58a0fb6..4df693308 100644 --- a/sprint-3-stretch-exercises/password-validator.test.js +++ b/sprint-3-stretch-exercises/password-validator.test.js @@ -16,11 +16,41 @@ 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 = "Aa1!"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("password have at least one uppercase letter(A-Z)", () => { + const password = "hello123"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +test("password have at least one lowercase letter(a-z)", () => { + const password = "A123AA"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +test("password have at least one number(0-9)", () => { + const password = "RRRqqq"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +test("password have at least one special character(!#$%.*&)", () => { + const password = "R3ass3"; + const result = isValidPassword(password); + expect(result).toEqual(false); +}); + +test("rejects a previously used password", () => { + const password = "Hello123!"; + + expect(isValidPassword(password)).toEqual(true); + expect(isValidPassword(password)).toEqual(false); +});