-
-
Notifications
You must be signed in to change notification settings - Fork 322
London | ITP-Jan-26 | Mohsen Zamani | Sprint 3 | stretch #939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| function creditCardValidator(cardNum) { | ||
| // Splits the cardNum into an array of characters | ||
| const cardNumArray = cardNum.split(""); | ||
|
|
||
| // Checks if length is 16, otherwise return false | ||
| if (cardNumArray.length !== 16) return false; | ||
|
|
||
| // checks if all digits are numbers | ||
| if (!cardNumArray.every((num) => num >= "0" && num <= "9")) return false; | ||
|
|
||
| // Checks if there are at least two different digits | ||
| const count = new Set(cardNumArray); | ||
| if (count.size < 2) return false; | ||
|
|
||
| // Checks if the sum of all digits is greater than 16 | ||
| const sumOfDigits = cardNumArray.reduce((acc, curr) => acc + Number(curr), 0); | ||
| if (sumOfDigits <= 16) return false; | ||
|
|
||
| //checks if last digit is even | ||
| if (cardNumArray[cardNumArray.length - 1] % 2 !== 0) return false; | ||
| return true; | ||
| } | ||
| console.log(creditCardValidator("1111111111111111")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,13 @@ | ||
| function passwordValidator(password) { | ||
| return password.length < 5 ? false : true | ||
| } | ||
| function passwordValidator(password, previousPasswords = []) { | ||
| if (!Array.isArray(previousPasswords)) return false; | ||
|
|
||
| if (password.length < 5) return false; | ||
| if (previousPasswords.includes(password)) return false; | ||
| previousPasswords.push(password); | ||
|
|
||
| const rules = [/[A-Z]/, /[a-z]/, /[0-9]/, /[!#$%.*&]/]; | ||
|
|
||
| return rules.every((rule) => rule.test(password)); | ||
| } | ||
|
|
||
| module.exports = passwordValidator; | ||
| module.exports = passwordValidator; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,12 +15,74 @@ 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"); | ||
| test("password has at least 5 characters", () => { | ||
| // Arrange | ||
| const password = "12345"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| } | ||
| ); | ||
| test("should return false if password has fewer than 5 characters", () => { | ||
| // Arrange | ||
| const password = "A1b&"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("should return false if password was previously used", () => { | ||
| // Arrange | ||
| const password = "5B43n21!"; | ||
| // Act | ||
| const result = isValidPassword(password, "5B43n21!"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expected this test to fail because the second parameter isn't an array of strings. I didn't realise string also has a method However, other implementations that expect the second parameter to be an array may fail this test unexpectedly.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Appreciate all the comments. |
||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("should return false if password does not contain an uppercase English letter", () => { | ||
| // Arrange | ||
| const password = "1a2345&"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("should return false if password does not contain an lowercase English letter", () => { | ||
| // Arrange | ||
| const password = "1B2345%"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("should return false if password has no digit", () => { | ||
| // Arrange | ||
| const password = "se!rjJN%Gk"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test('should return false if password has no special characters including "!", "#", "$", "%", ".", "*", "&"', () => { | ||
| // Arrange | ||
| const password = "sdkerjJNG23k"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("should return true if password meets all validation rules", () => { | ||
| // Arrange | ||
| const password = "sdkerj!JNG23k&"; | ||
| // Act | ||
| const result = isValidPassword(password, ["se!rjJN%G6k", "1B2h345%a"]); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| test("should return false if previousPassword is not an array", () => { | ||
| // Arrange | ||
| const password = "sdkerj!JNG23k&"; | ||
| // Act | ||
| const result = isValidPassword(password, "1B2h345%a"); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make the test descriptions more detailed and descriptive? That way, if a test fails, the person implementing the function can quickly understand what went wrong and why.
Right now, for example, "password contains at least one number (0-9)" does not quite indicate what the test is testing. (Did you mean to say "not containing")