diff --git a/Sprint-3/3-stretch/card-validator.md b/Sprint-3/3-stretch/card-validator.md deleted file mode 100644 index e39c6ace6..000000000 --- a/Sprint-3/3-stretch/card-validator.md +++ /dev/null @@ -1,35 +0,0 @@ -## **PROJECT: Credit Card Validator** - -In this project you'll write a script that validates whether or not a credit card number is valid. - -Here are the rules for a valid number: - -- 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. - -For example, the following credit card numbers are valid: - -```markdown -9999777788880000 -6666666666661666 -``` - -And the following credit card numbers are invalid: - -```markdown -a92332119c011112 (invalid characters) -4444444444444444 (only one type of number) -1111111111111110 (sum less than 16) -6666666666666661 (odd final number) -``` - -These are the requirements your project needs to fulfill: - -- Make a JavaScript file with a name that describes its contents. -- Create a function with a descriptive name which makes it clear what the function does. The function should take one argument, the credit card number to validate. -- Write at least 2 comments that explain to others what a line of code is meant to do. -- Return a boolean from the function to indicate whether the credit card number is valid. - -Good luck! diff --git a/Sprint-3/3-stretch/find.js b/Sprint-3/3-stretch/find.js index c7e79a2f2..a80a1ea19 100644 --- a/Sprint-3/3-stretch/find.js +++ b/Sprint-3/3-stretch/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 +// In the beginning of the function it is set to 0, then each time the while loop runs it increases by 1 until it reaches the length of the string . // b) What is the if statement used to check +// The if statement checks if the character at the current index of the string is equal to the character we are searching for. If it is, the function returns the current index. // c) Why is index++ being used? +// The index++ is used to move to the next character in the string after each iteration of the while loop. // d) What is the condition index < str.length used for? +// The condition index < str.length is used to ensure that the while loop continues to run as long as there are characters left to check in the string. diff --git a/Sprint-3/3-stretch/password-validator.js b/Sprint-3/3-stretch/password-validator.js index b55d527db..fa1596eac 100644 --- a/Sprint-3/3-stretch/password-validator.js +++ b/Sprint-3/3-stretch/password-validator.js @@ -1,6 +1,30 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true -} +const passwords = ["Abcde1!", "Hello#2"]; +function passwordValidator(password) { + if (password.length < 5) { + return false; + } + + if (!/[A-Z]/.test(password)) { + return false; + } + + if (!/[a-z]/.test(password)) { + return false; + } + + if (!/[0-9]/.test(password)) { + return false; + } + if (!/[!#$%&? "]/.test(password)) { + return false; + } + if (passwords.includes(password)) { + return false; + } + + + return true; +} module.exports = 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..0188c9467 100644 --- a/Sprint-3/3-stretch/password-validator.test.js +++ b/Sprint-3/3-stretch/password-validator.test.js @@ -21,6 +21,59 @@ test("password has at least 5 characters", () => { // Act const result = isValidPassword(password); // Assert + expect(result).toEqual(false); +} +); + +// passsword has at least one uppercase letter +test("password has at least one uppercase letter", () => { + + const password = "abcd!1"; + const result = isValidPassword(password); + expect(result).toEqual(false); +} +); + +// password has at least one lowercase letter +test("password has at least one lowercase letter", () => { + + const password = "ABCD!1"; + const result = isValidPassword(password); + expect(result).toEqual(false); +} +); + +// password has at least one number +test("password has at least one number", () => { + + const password = "Abcd!@"; + const result = isValidPassword(password); + expect(result).toEqual(false); +} +); + +// password has at least one symbol: +test("password has at least one symbol: (!, #, $, %, ., *, &)", () => { + + const password = "Abcd12"; + const result = isValidPassword(password); + expect(result).toEqual(false); +} +); + +// password must not be any previous password in the passwords array. +test("password must not be any previous password in the passwords array.", () => { + + const password = "abcd!1"; + const result = isValidPassword(password); + expect(result).toEqual(false); +} +); +// password is valid when all requirements are met +test ("password is valid when it meets all requirements",() =>{ + + const password = "Abcd!2"; + const result = isValidPassword(password); expect(result).toEqual(true); } -); \ No newline at end of file +);