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/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/card-validator.js b/Sprint-3/3-stretch/card-validator.js new file mode 100644 index 000000000..1cc4c89c4 --- /dev/null +++ b/Sprint-3/3-stretch/card-validator.js @@ -0,0 +1,80 @@ +//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 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 + } +} + +module.exports = cardValidator; 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..547de2b15 --- /dev/null +++ b/Sprint-3/3-stretch/card-validator.test.js @@ -0,0 +1,20 @@ +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 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); } diff --git a/add-an-I.js b/add-an-I.js new file mode 100644 index 000000000..e3a5b1d98 --- /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"; + } + result += " ".repeat(i) + "I"; + } + return result; +} + +console.log(drawStairs(5)); 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"; +}