From e349088622cb365d0ffaf34cf1efcc27dcc4c4fd Mon Sep 17 00:00:00 2001 From: Pythia Date: Mon, 3 Nov 2025 19:39:32 +0100 Subject: [PATCH 1/9] Wrote functions in card-validator.js to validate credit card numbers. --- Sprint-3/3-stretch/card-validator.js | 77 +++++++++++++++++++++++ Sprint-3/3-stretch/card-validator.test.js | 0 2 files changed, 77 insertions(+) create mode 100644 Sprint-3/3-stretch/card-validator.js create mode 100644 Sprint-3/3-stretch/card-validator.test.js diff --git a/Sprint-3/3-stretch/card-validator.js b/Sprint-3/3-stretch/card-validator.js new file mode 100644 index 000000000..690215179 --- /dev/null +++ b/Sprint-3/3-stretch/card-validator.js @@ -0,0 +1,77 @@ +//Function to validate that input is 16 digit number +function isSixteenDigitNumber(cardNumber) { + const cardNumberLength = cardNumber.toString().length // Get the length of the input cardNumber + if (cardNumberLength === 16) { // Check if the length is exactly 16 + for (let i = 0; i < cardNumberLength; i++) { //Check each digit till the length of cardNumber + const char = cardNumber.toString().charAt(i); // The character at index i + if (isNaN(char)) { //If the character is not a number + return "Input only numbers"; // Return error message + } + else { + continue; // Continue checking the next character + } + } + return true; // All characters are digits and length is 16 + } + else { + return "Input 16 digits card number"; // Length is not 16 + } +} + + +// Function to validate that the card number has at least two different digits +function hasTwoDifferentDigits(cardNumber) { + const firstDigit = cardNumber.toString().charAt(0); // Get the first digit of the card number + for (let i = 1; i< cardNumber.toString().length; i++) { + const digit = cardNumber.toString().charAt(i); // Get the digit at index i + if (digit !== firstDigit) { + return true; // At least two different digits found + } + } + return false; // All digits are the same + } + + +// Function to check if the last digit of the card number is even +function lastDigitIsEven(cardNumber){ + const lastDigit = cardNumber.toString().charAt(15); // Get the last digit of the card number + if (Number(lastDigit) % 2 === 0) { + return true; // Last digit is even + } + else { + return false; // Last digit is odd + } +} + +//Function to calculate the sum of all digits to be > 16 +function sumOfDigitsGreaterThan16(cardNumber) { + let sum = 0; + for (let i = 0; i < cardNumber.toString().length; i++) { + const digit = Number(cardNumber.toString().charAt(i)); // Get the digit at index i and convert to number + sum += digit; // Add the digit to the sum + } + if (sum > 16) { + return true; // Sum of digits is greater than 16 + } + else { + return false; // Sum of digits is not greater than 16 + } +} + + +// Main function to validate the card number +function card-validator(cardNumber) { + if (isSixteenDigitNumber(cardNumber) === true) { + if hasTwoDifferentDigits(cardNumber) && lastDigitIsEven(cardNumber) && sumOfDigitsGreaterThan16(cardNumber) { + return "Valid card number"; // All validations passed + } + else { + return "Invalid card number"; // One or more validations failed + } + } + else { + return isSixteenDigitNumber(cardNumber); // Return the error message from isSixteenDigitNumber function + } +} + +module.exports = card-validator; \ No newline at end of file diff --git a/Sprint-3/3-stretch/card-validator.test.js b/Sprint-3/3-stretch/card-validator.test.js new file mode 100644 index 000000000..e69de29bb From ea661c3a5da765c741d04353594def6858eaf33e Mon Sep 17 00:00:00 2001 From: Pythia Date: Mon, 3 Nov 2025 20:01:48 +0100 Subject: [PATCH 2/9] Fixed identation and change variable name --- Sprint-3/3-stretch/card-validator.js | 95 ++++++++++++----------- Sprint-3/3-stretch/card-validator.test.js | 2 + 2 files changed, 51 insertions(+), 46 deletions(-) diff --git a/Sprint-3/3-stretch/card-validator.js b/Sprint-3/3-stretch/card-validator.js index 690215179..1cc4c89c4 100644 --- a/Sprint-3/3-stretch/card-validator.js +++ b/Sprint-3/3-stretch/card-validator.js @@ -1,45 +1,46 @@ //Function to validate that input is 16 digit number function isSixteenDigitNumber(cardNumber) { - const cardNumberLength = cardNumber.toString().length // Get the length of the input cardNumber - if (cardNumberLength === 16) { // Check if the length is exactly 16 - for (let i = 0; i < cardNumberLength; i++) { //Check each digit till the length of cardNumber - const char = cardNumber.toString().charAt(i); // The character at index i - if (isNaN(char)) { //If the character is not a number - return "Input only numbers"; // Return error message - } - else { - continue; // Continue checking the next character - } + const cardNumberLength = cardNumber.toString().length; // Get the length of the input cardNumber + if (cardNumberLength === 16) { + // Check if the length is exactly 16 + for (let i = 0; i < cardNumberLength; i++) { + //Check each digit till the length of cardNumber + const char = cardNumber.toString().charAt(i); // The character at index i + if (isNaN(char)) { + //If the character is not a number + return "Input only numbers"; // Return error message + } + else { + continue; // Continue checking the next character } - return true; // All characters are digits and length is 16 } + return true; // All characters are digits and length is 16 + } else { - return "Input 16 digits card number"; // Length is not 16 + return "Input 16 digits card number"; // Length is not 16 } } - // Function to validate that the card number has at least two different digits function hasTwoDifferentDigits(cardNumber) { - const firstDigit = cardNumber.toString().charAt(0); // Get the first digit of the card number - for (let i = 1; i< cardNumber.toString().length; i++) { - const digit = cardNumber.toString().charAt(i); // Get the digit at index i - if (digit !== firstDigit) { - return true; // At least two different digits found - } + const firstDigit = cardNumber.toString().charAt(0); // Get the first digit of the card number + for (let i = 1; i < cardNumber.toString().length; i++) { + const digit = cardNumber.toString().charAt(i); // Get the digit at index i + if (digit !== firstDigit) { + return true; // At least two different digits found } - return false; // All digits are the same - } - +} + return false; // All digits are the same +} // Function to check if the last digit of the card number is even -function lastDigitIsEven(cardNumber){ - const lastDigit = cardNumber.toString().charAt(15); // Get the last digit of the card number +function lastDigitIsEven(cardNumber) { + const lastDigit = cardNumber.toString().charAt(15); // Get the last digit of the card number if (Number(lastDigit) % 2 === 0) { - return true; // Last digit is even - } + return true; // Last digit is even + } else { - return false; // Last digit is odd + return false; // Last digit is odd } } @@ -47,31 +48,33 @@ function lastDigitIsEven(cardNumber){ function sumOfDigitsGreaterThan16(cardNumber) { let sum = 0; for (let i = 0; i < cardNumber.toString().length; i++) { - const digit = Number(cardNumber.toString().charAt(i)); // Get the digit at index i and convert to number - sum += digit; // Add the digit to the sum + const digit = Number(cardNumber.toString().charAt(i)); // Get the digit at index i and convert to number + sum += digit; // Add the digit to the sum } if (sum > 16) { - return true; // Sum of digits is greater than 16 - } + return true; // Sum of digits is greater than 16 + } else { - return false; // Sum of digits is not greater than 16 - } + return false; // Sum of digits is not greater than 16 + } } - // Main function to validate the card number -function card-validator(cardNumber) { - if (isSixteenDigitNumber(cardNumber) === true) { - if hasTwoDifferentDigits(cardNumber) && lastDigitIsEven(cardNumber) && sumOfDigitsGreaterThan16(cardNumber) { - return "Valid card number"; // All validations passed - } - else { - return "Invalid card number"; // One or more validations failed - } - } +function cardValidator(cardNumber) { + if (isSixteenDigitNumber(cardNumber) === true) { + if ( + hasTwoDifferentDigits(cardNumber) && + lastDigitIsEven(cardNumber) && + sumOfDigitsGreaterThan16(cardNumber) + ) { + return "Valid card number"; // All validations passed + } else { + return "Invalid card number"; // One or more validations failed + } + } else { - return isSixteenDigitNumber(cardNumber); // Return the error message from isSixteenDigitNumber function - } + return isSixteenDigitNumber(cardNumber); // Return the error message from isSixteenDigitNumber function + } } -module.exports = card-validator; \ No newline at end of file +module.exports = cardValidator; diff --git a/Sprint-3/3-stretch/card-validator.test.js b/Sprint-3/3-stretch/card-validator.test.js index e69de29bb..ff80d9846 100644 --- a/Sprint-3/3-stretch/card-validator.test.js +++ b/Sprint-3/3-stretch/card-validator.test.js @@ -0,0 +1,2 @@ +const cardValidator = require("./card-validator"); +test; From c7cd7bcb64484f80fbef9f8c776eb6765fbb8930 Mon Sep 17 00:00:00 2001 From: Pythia Date: Mon, 3 Nov 2025 20:21:28 +0100 Subject: [PATCH 3/9] Created one test --- Sprint-3/3-stretch/card-validator.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-3/3-stretch/card-validator.test.js b/Sprint-3/3-stretch/card-validator.test.js index ff80d9846..29e1981b1 100644 --- a/Sprint-3/3-stretch/card-validator.test.js +++ b/Sprint-3/3-stretch/card-validator.test.js @@ -1,2 +1,4 @@ const cardValidator = require("./card-validator"); -test; +test (('valid 16digit card number passes validation'), () => { + expect(cardValidator(2549876514523548)).toEqual("Valid card number") +}); From f68322a693bd87547cf6bca31d84f1fce8ed515c Mon Sep 17 00:00:00 2001 From: Pythia Date: Tue, 4 Nov 2025 12:54:51 +0100 Subject: [PATCH 4/9] Wrote more tests on my test file --- Sprint-3/3-stretch/card-validator.test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sprint-3/3-stretch/card-validator.test.js b/Sprint-3/3-stretch/card-validator.test.js index 29e1981b1..547de2b15 100644 --- a/Sprint-3/3-stretch/card-validator.test.js +++ b/Sprint-3/3-stretch/card-validator.test.js @@ -2,3 +2,19 @@ const cardValidator = require("./card-validator"); test (('valid 16digit card number passes validation'), () => { expect(cardValidator(2549876514523548)).toEqual("Valid card number") }); + +test (('16 digit card number that has an odd last digit'), () => { + expect(cardValidator(2547896254356557)).toEqual('Invalid card number') +}); + +test (('16 digit card number with all same digits'), () => { + expect(cardValidator(1111111111111111)).toEqual('Invalid card number') +}); + +test (('16 digit card number with at least two different digits, last digit even,sum off digits < 16'), () => { + expect(cardValidator(1010101010101012)).toEqual('Invalid card number') +}); + +test (('16 digit card number with not only numbers'), () => { + expect(cardValidator('1df0877999954677')).toEqual('Input only numbers') +}); \ No newline at end of file From ae7984f32ccc5b30cb7e3c5e125409c1385aef20 Mon Sep 17 00:00:00 2001 From: Pythia Date: Tue, 4 Nov 2025 15:18:37 +0100 Subject: [PATCH 5/9] Wrote the code to meet conditions and tested it till is correct. Fixed typos --- .../implement/3-get-card-value.js | 1 + Sprint-3/3-stretch/password-validator.js | 80 ++++++++++++++++++- Sprint-3/3-stretch/password-validator.test.js | 14 +++- 3 files changed, 89 insertions(+), 6 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index 266525d1b..a4e22eefe 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -8,6 +8,7 @@ // write one test at a time, and make it pass, build your solution up methodically // just make one change at a time -- don't rush -- programmers are deep and careful thinkers function getCardValue(card) { + const rank = card.slice(0,-1) //removes the last digit if (rank === "A") { return 11; } diff --git a/Sprint-3/3-stretch/password-validator.js b/Sprint-3/3-stretch/password-validator.js index b55d527db..bdbd4acb2 100644 --- a/Sprint-3/3-stretch/password-validator.js +++ b/Sprint-3/3-stretch/password-validator.js @@ -1,6 +1,80 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true +//Has at least 5 digits +function isAtLeastFive(password) { + if (password.length >= 5) { + return true; + } + return false; } +//Has at least one upper case English letter +function hasAtLeastOneUpperCase(password) { + let index = 0; //starts from first index location to check + while (index < password.length) { //When our location is smaller than the total str length + const char = password.charAt(index); //This is the location of each digit at the moment of the loop + if (char >= "A" && char <= "Z"); //To include all alphabet in capital letters in at least one location + return true; + index++ //Moves loop to next digit for checking + } + return false; //If loop is finished and there were no capitals found +} + +//Has at least one lower letter in English +function hasAtLeastOneLowerCase(password) { + let index = 0; //starts from first index location to check + while (index < password.length) { //When our location is smaller than the total str length + const char = password.charAt(index); //This is the location of each digit at the moment of the loop + if (char >= "a" && char <= "z"); //To include all alphabet in lower letters in at least one location + return true; + index++ //Moves loop to next digit for checking + } + return false; //If loop is finished and there were no lower letters found +} + +//Has at least one number from 0 to 9 +function hasAtLeastOneNumber(password) { + let index = 0; //starts from first index location to check + while (index < password.length) { //When our location is smaller than the total str length + const char = password.charAt(index); //This is the location of each digit at the moment of the loop + if (char >= "0" && char <= "9"); //To include all numbers in at least one location + return true; + index++ //Moves loop to next digit for checking + } + return false; //If loop is finished and there were no numbers found +} + +//Has at least one special character "!", "#", "$", "%", ".", "*", "&" +function hasAtLeastOneSpecialCharacter(password) { + const specialCharacter = ["!", "#", "$", "%", "*", "&"] + let index = 0; + while (index < password.length) { + const char = password.charAt(index); //This is the location of each digit at the moment of the loop + if (specialCharacter.includes(char)); //To include a symbol in at least one location + return true; + index++ //Moves loop to next digit for checking + } + return false; //If loop is finished and there were no symbols found +} + +//Must not be a previous password in passwords array +function isNewPassword(password, passwords) { //Password is the new input, passwords are the previous array + return !passwords.includes(password); //!is a negative statement so it will return false if new password was used before +} + + +//Has at least 5 digits & all above conditions are met +function passwordValidator(password, passwords) { + return (isAtLeastFive && + hasAtLeastOneUpperCase(password) && + hasAtLeastOneLowerCase(password) && + hasAtLeastOneNumber(password) && + hasAtLeastOneSpecialCharacter(password) && + isNewPassword(password, passwords)); +} -module.exports = passwordValidator; \ No newline at end of file +module.exports = {isAtLeastFive, + hasAtLeastOneUpperCase, + hasAtLeastOneLowerCase, + hasAtLeastOneNumber, + hasAtLeastOneSpecialCharacter, + isNewPassword, + passwordValidator} \ No newline at end of file diff --git a/Sprint-3/3-stretch/password-validator.test.js b/Sprint-3/3-stretch/password-validator.test.js index 8fa3089d6..6c1963464 100644 --- a/Sprint-3/3-stretch/password-validator.test.js +++ b/Sprint-3/3-stretch/password-validator.test.js @@ -14,12 +14,20 @@ 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 */ -const isValidPassword = require("./password-validator"); +const {isAtLeastFive, + hasAtLeastOneUpperCase, + hasAtLeastOneLowerCase, + hasAtLeastOneNumber, + hasAtLeastOneSpecialCharacter, + isNewPassword, + passwordValidator} = require("./password-validator"); + test("password has at least 5 characters", () => { // Arrange - const password = "12345"; + const password = "1Ab4&"; + const passwords = ["gJ25!", "15#Ki", "6z9F*"] // Act - const result = isValidPassword(password); + const result = passwordValidator(password, passwords); // Assert expect(result).toEqual(true); } From 83bea1aaa662866f29f853f0256de05c6fd39dc2 Mon Sep 17 00:00:00 2001 From: Pythia Date: Sun, 9 Nov 2025 13:36:29 +0100 Subject: [PATCH 6/9] Created a file for the class --- elevator.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 elevator.js diff --git a/elevator.js b/elevator.js new file mode 100644 index 000000000..9e916d2bb --- /dev/null +++ b/elevator.js @@ -0,0 +1,8 @@ +function elevatorCloser(left, call, right) { + const leftFloor = Math.abs(left - call); + const rightFloor = Math.abs(right - call); + if (leftFloor >= rightFloor) { + return "right"; + } + return "left"; +} From 4cafea9a58823553b26ac3b6a94e9f84940876fd Mon Sep 17 00:00:00 2001 From: Pythia Date: Tue, 11 Nov 2025 14:55:38 +0100 Subject: [PATCH 7/9] Draw stairs function testing --- .prettierrc | 2 +- add-an-I.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 add-an-I.js diff --git a/.prettierrc b/.prettierrc index 59bb3b44f..264aee094 100644 --- a/.prettierrc +++ b/.prettierrc @@ -12,7 +12,7 @@ "requirePragma": false, "semi": true, "singleQuote": false, - "tabWidth": 2, + "tabWidth": 4, "trailingComma": "es5", "useTabs": false, "vueIndentScriptAndStyle": false diff --git a/add-an-I.js b/add-an-I.js new file mode 100644 index 000000000..3ae394a3f --- /dev/null +++ b/add-an-I.js @@ -0,0 +1,12 @@ +function drawStairs(n) { + let result = ""; + for (let i = 0; i < n; i++) { + if (i > 0) { + result += "\n"; + } + return (result += " ".repeat(n) + "I"); + } + return result; +} + +console.log(drawStairs(5)); From 1ecfe787f0db99e912bd4f057798b621d05500ac Mon Sep 17 00:00:00 2001 From: Pythia Date: Tue, 11 Nov 2025 14:58:36 +0100 Subject: [PATCH 8/9] Fixed typos --- add-an-I.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/add-an-I.js b/add-an-I.js index 3ae394a3f..34e676076 100644 --- a/add-an-I.js +++ b/add-an-I.js @@ -4,7 +4,7 @@ function drawStairs(n) { if (i > 0) { result += "\n"; } - return (result += " ".repeat(n) + "I"); + result += " ".repeat(n) + "I"; } return result; } From 52b75cc1b4afecc2ac6e870d55e5e9866323bf19 Mon Sep 17 00:00:00 2001 From: Pythia Date: Tue, 11 Nov 2025 15:00:59 +0100 Subject: [PATCH 9/9] repeat fixed to be looping i times not n times --- add-an-I.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/add-an-I.js b/add-an-I.js index 34e676076..e3a5b1d98 100644 --- a/add-an-I.js +++ b/add-an-I.js @@ -4,7 +4,7 @@ function drawStairs(n) { if (i > 0) { result += "\n"; } - result += " ".repeat(n) + "I"; + result += " ".repeat(i) + "I"; } return result; }