From 4f63a8d57998eb115027821a5a6b9a2f9c996eb3 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 29 Jun 2026 08:17:03 +0100 Subject: [PATCH 1/2] London | 26-ITP-May | Yonatan Teklemariam | Sprint 3 | Strech-credit-card-validator --- Sprint-3/4-stretch/Credit-Card-Validator.js | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Sprint-3/4-stretch/Credit-Card-Validator.js diff --git a/Sprint-3/4-stretch/Credit-Card-Validator.js b/Sprint-3/4-stretch/Credit-Card-Validator.js new file mode 100644 index 0000000000..e4d9a97aad --- /dev/null +++ b/Sprint-3/4-stretch/Credit-Card-Validator.js @@ -0,0 +1,43 @@ +function validateCreditCard(cardNumber) { + if (cardNumber.length !== 16) { + return false; // this line of code checks whether the card number is 16 digits long or not + } + let index = 0; + while (index < cardNumber.length) { + if (cardNumber[index] < "0" || cardNumber[index] > "9") { + return false; // this line of code checks whether the card number is composed of digits not other characters + } + index++; + } + const firstDigit = cardNumber[0]; // first digit of the card number is stored for the purpose of checking up whether two digits are identical or not + + index = 1; + while (index < cardNumber.length) { + if (cardNumber[index] !== firstDigit) { + break; // loop ends immediately here to give way to the next check + } + index++; + } + if (index === cardNumber.length) { + return false; + } + const lastDigit = cardNumber.slice(-1); + const lastNumber = Number(lastDigit); + + if (lastNumber % 2 !== 0) { + return false; // this line of code checks whether the last digit of the card number is divisible by 2 in other words "even" or not + } + + let total = 0; + index = 0; + while (index < cardNumber.length) { + total += Number(cardNumber[index]); + index++; + } + if (total <= 16) { + return false; // this line of code checks whether the sum of all the digits in the card number is more than 16 or not + } + + return true; +} +console.log(validateCreditCard("9999777788880000")); From 4e34493f6912d0bf55dac07e5f0a97422a27f678 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Sat, 4 Jul 2026 07:50:36 +0100 Subject: [PATCH 2/2] written test jests for password validator to check based on the given requirements --- Sprint-3/4-stretch/find.js | 7 +++ Sprint-3/4-stretch/password-validator.js | 28 +++++++++-- Sprint-3/4-stretch/password-validator.test.js | 49 +++++++++++++++++++ 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/Sprint-3/4-stretch/find.js b/Sprint-3/4-stretch/find.js index c7e79a2f21..af1a13e514 100644 --- a/Sprint-3/4-stretch/find.js +++ b/Sprint-3/4-stretch/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 +// During the call to find(str, char): index starts at 0 and on each loop iteration, the function checks if str[index] is equal to char. if it is not, index is incremented by 1 (index++) and the loop continues until either a match is found or index reaches str.length. If a match is found, the function returns the current value of index. If no match is found after checking all characters, the function returns -1. +// C O D E Y O "U" R F U T U R E +// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 => MATCH FOUND --> return index = 7 + // b) What is the if statement used to check +// The if statement checks if str[index] is equal to char. This implies whether the current character in the string matches the character we are searching for. If it does, the function returns the current index. If not, the loop continues to the next character. // c) Why is index++ being used? +// index++ is used to increment the index variable by 1 after each loop iteration and this is done to move to the next character in the string for comparison with char. This enables the function to check each character in the string sequentially until a match is found or the end of the string is reached. // d) What is the condition index < str.length used for? +// The condition index < str.length is used to ensure that the loop continues to run as long as the index is less than the length of the string. This prevents the function from trying to run indefinitely and ensures that it only checks valid indices within the bonds of a given string. Therefore, the loop will terminate when index reaches str.length, indicating that all characters have been checked without finding a match. diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index b55d527dba..3c22804b67 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -1,6 +1,28 @@ function passwordValidator(password) { - return password.length < 5 ? false : true + // must be at least 5 characters long + if (password.length < 5) { + return false; + } + // must contain at least one lowercase letter + const hasLowercase = /[a-z]/.test(password); + // must contain at least one uppercase letter + const hasUppercase = /[A-Z]/.test(password); + // must contain at least one digit + const hasDigit = /[0-9]/.test(password); + // must contain at least one special character + const hasSpecialChar = /[!@#$%^&*()\-+]/.test(password); + // must not contain any spaces + const hasNoSpaces = !/\s/.test(password); + // must not be any previous password in the passwords array + const isNewPassword = !previouspasswords.includes(password); + return ( + hasLowercase && + hasUppercase && + hasDigit && + hasSpecialChar && + hasNoSpaces && + isNewPassword + ); } - -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..9a8bc39f00 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -11,6 +11,7 @@ To be valid, a password must: - 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. +-Must not contain any spaces. You must breakdown this problem in order to solve it. Find one test case first and get that working */ @@ -23,4 +24,52 @@ test("password has at least 5 characters", () => { // Assert expect(result).toEqual(true); } +test("password has at least one English uppercase letter (A-Z)", () => { + // Arrange + const password = "1A345"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +} +test("password has at least one English lowercase letter (a-z)", () => { + // Arrange + const password = "1a345"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +} +test("password has at least one number (0-9)", () => { + // Arrange + const password = "Abcd5"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +} +test("password has at least one special character", () => { + // Arrange + const password = "1234!"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +} +test("password has no spaces", () => { + // Arrange + const password = "Abc 5$"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +} +test("password is not a previous password", () => { + // Arrange + const password = "12345"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +} ); \ No newline at end of file