diff --git a/Sprint-3/4-stretch/card-validator.js b/Sprint-3/4-stretch/card-validator.js new file mode 100644 index 0000000000..cd9a2d478e --- /dev/null +++ b/Sprint-3/4-stretch/card-validator.js @@ -0,0 +1,38 @@ +function validateCreditCardNumber(cardNumber) { + // Check if the card number is a positive integer. + if (typeof cardNumber !== "number" || cardNumber < 0) { + return false; + } + + const cardNumberString = String(cardNumber); + // Check if the card number has at least two different digits + const uniqueDigits = new Set(cardNumberString); + + if (uniqueDigits.size < 2) { + return false; + } + + // Check if the card number has exactly 16 digits. + if (cardNumberString.length !== 16) { + return false; + } + + // Check if the last digit is even + if (Number(cardNumberString.slice(-1)) % 2 !== 0) { + return false; + } + + // Check if the sum of all the digits is greater than 16 + const sumOfAllDigits = Array.from(cardNumberString).reduce( + (sum, digit) => sum + Number(digit), + 0 + ); + + if (sumOfAllDigits <= 16) { + return false; + } + + return true; +} + +module.exports = validateCreditCardNumber; diff --git a/Sprint-3/4-stretch/card-validator.test.js b/Sprint-3/4-stretch/card-validator.test.js new file mode 100644 index 0000000000..c408f05f74 --- /dev/null +++ b/Sprint-3/4-stretch/card-validator.test.js @@ -0,0 +1,91 @@ +const validateCreditCardNumber = require("./card-validator"); + +test("Number must be 16 digits, all of them must be numbers.", () => { + // Arrange + const cardNumber = 1234567890123456; + // Act + const result = validateCreditCardNumber(cardNumber); + // Assert + expect(result).toEqual(true); +}); + +test("Credit card number must have at least two different digits.", () => { + // Arrange + const cardNumber = 6262826262628262; + // Act + const result = validateCreditCardNumber(cardNumber); + // Assert + expect(result).toEqual(true); +}); + +test("Credit card with only one repeating digit is invalid.", () => { + // Arrange + const cardNumber = 8888888888888888; + // Act + const result = validateCreditCardNumber(cardNumber); + // Assert + expect(result).toEqual(false); +}); + +test("Credit card number cannot have an odd last digit.", () => { + // Arrange + const cardNumber = 1234567890123457; + // Act + const result = validateCreditCardNumber(cardNumber); + // Assert + expect(result).toEqual(false); +}); + +test("Credit card number sum of all digits cannot be less than or equal to 16.", () => { + // Arrange + const cardNumber = 1000000000000000; + // Act + const result = validateCreditCardNumber(cardNumber); + // Assert + expect(result).toEqual(false); +}); + +test("Credit card number sum of all digits must be greater than 16.", () => { + // Arrange + const cardNumber = 1234567890123452; + // Act + const result = validateCreditCardNumber(cardNumber); + // Assert + expect(result).toEqual(true); +}); + +test("Credit card number must not be negative.", () => { + // Arrange + const cardNumber = -1234567890123456; + // Act + const result = validateCreditCardNumber(cardNumber); + // Assert + expect(result).toEqual(false); +}); + +test("Credit card number must be a number.", () => { + // Arrange + const cardNumber = "1234567890123456"; + // Act + const result = validateCreditCardNumber(cardNumber); + // Assert + expect(result).toEqual(false); +}); + +test("Credit card number must have exactly 16 digits.", () => { + // Arrange + const cardNumber = 212222222212222; + // Act + const result = validateCreditCardNumber(cardNumber); + // Assert + expect(result).toEqual(false); +}); + +test("Credit card number cannot have less or more than 16 digits.", () => { + // Arrange + const cardNumber = 212222222212222; + // Act + const result = validateCreditCardNumber(cardNumber); + // Assert + expect(result).toEqual(false); +}); diff --git a/Sprint-3/4-stretch/find.js b/Sprint-3/4-stretch/find.js index c7e79a2f21..49884e3db3 100644 --- a/Sprint-3/4-stretch/find.js +++ b/Sprint-3/4-stretch/find.js @@ -20,6 +20,19 @@ console.log(find("code your future", "z")); // Pay particular attention to the following: // a) How the index variable updates during the call to find +// The index variable starts at 0 and increments by 1 in each iteration of the while loop. +// In case of the call find("code your future", "u"), the index variable updates as follows: +// 0, 1, 2, 3, 4, 5, 6, 7 - when it stops, because the character at index 7 is "u" +// In case of the call find("code your future", "z"), the index variable updates as follows: +// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 - when it stops, because the condition index < str.length is no longer true +// the and because the loop has arrived to the end of the string without finding "z". + // b) What is the if statement used to check +// The if statement is used to check if the character at the current index is equal to the character we are looking for. + // c) Why is index++ being used? +// index++ is incrementing the index by 1 in each iteration of the while loop. this is how the index is being moved from 0 to the end of the string, or the index where the character is found. + // d) What is the condition index < str.length used for? +// It gives the limit to the looping, that is, the index must be less than the length of the string, and in case the index is equal or greater than the string length, the loop will stop. +// If this condition wasn't given, the loop would continue to run indefinitely. diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index b55d527dba..4eee8c3034 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -1,6 +1,24 @@ function passwordValidator(password) { - return password.length < 5 ? false : true + /* To be valid, a password must: + - Have at least 5 characters. + - Have at least one English uppercase letter (A-Z) + - Have at least one English lowercase letter (a-z) + - Have at least one number (0-9) + - Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&") + - Must not be any previous password in the passwords array. + */ + const passwords = ["pa$$w0rd", "Qwerty1#", "Adm1n2#", "$3cr4t"]; + if ( + password.length < 5 || + !/[A-Z]/.test(password) || + !/[a-z]/.test(password) || + !/[0-9]/.test(password) || + !/[!#\$%\.\*&]/.test(password) || + passwords.includes(password) + ) { + return false; + } + return true; } - -module.exports = passwordValidator; \ No newline at end of file +module.exports = passwordValidator; diff --git a/Sprint-3/4-stretch/password-validator.test.js b/Sprint-3/4-stretch/password-validator.test.js index 8fa3089d6b..091e832280 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -16,11 +16,100 @@ You must breakdown this problem in order to solve it. Find one test case first a */ 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 = "12345cA!"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +}); + +test("password with less than 5 characters is invalid", () => { + // Arrange + const password = "1Aa%"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("password has at least one English uppercase letter (A-Z)", () => { + // Arrange + const password = "1234bA$"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +}); + +test("password with no uppercase letters is invalid", () => { + // Arrange + const password = "1234ab$"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("password has at least one English lowercase letter (a-z)", () => { + // Arrange + const password = "1234Aa%"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +}); + +test("password with no lowercase letters is invalid", () => { + // Arrange + const password = "1234AB$"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("password has at least one number (0-9)", () => { + // Arrange + const password = "1234Aa&"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +}); + +test("password with no numbers is invalid", () => { + // Arrange + const password = "passWord!"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("password has at least one non-alphanumeric symbol: (!, #, $, %, ., *, &)", () => { + // Arrange + const password = "1234aA#"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +}); + +test("must not be any previous password in the passwords array.", () => { + // Arrange + const password = "pas$W0rd"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +}); + +test("previous passwords in the passwords array are invalid", () => { + // Arrange + const password = "Qwerty1#"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +});