generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 338
Sheffield | 26-ITP-jan | Richard Frimpong | Sprint 3 | Stretch Coursework #1250
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
Open
Richiealx
wants to merge
2
commits into
CodeYourFuture:main
Choose a base branch
from
Richiealx:coursework/sprint-3-stretch-redo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Validate whether a credit card number meets the rules from card-validator.md | ||
| function validateCreditCardNumber(cardNumber) { | ||
| // Rule 1: the value must be exactly 16 characters long and contain only digits. | ||
| if (!/^\d{16}$/.test(cardNumber)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Rule 2: not all digits can be the same. | ||
| const uniqueDigits = new Set(cardNumber); | ||
| if (uniqueDigits.size < 2) { | ||
| return false; | ||
| } | ||
|
|
||
| // Rule 3: the final digit must be even. | ||
| const lastDigit = Number(cardNumber[cardNumber.length - 1]); | ||
| if (lastDigit % 2 !== 0) { | ||
| return false; | ||
| } | ||
|
|
||
| // Rule 4: the sum of all digits must be greater than 16. | ||
| let sum = 0; | ||
| for (const digit of cardNumber) { | ||
| sum += Number(digit); | ||
| } | ||
|
|
||
| if (sum <= 16) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| module.exports = validateCreditCardNumber; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| const validateCreditCardNumber = require("./card-validator"); | ||
|
|
||
| test("returns true for a valid card number", () => { | ||
| expect(validateCreditCardNumber("9999777788880000")).toEqual(true); | ||
| expect(validateCreditCardNumber("6666666666661666")).toEqual(true); | ||
| }); | ||
|
|
||
| test("returns false when the card contains non-digit characters", () => { | ||
| expect(validateCreditCardNumber("a92332119c011112")).toEqual(false); | ||
| }); | ||
|
|
||
| test("returns false when all digits are the same", () => { | ||
| expect(validateCreditCardNumber("4444444444444444")).toEqual(false); | ||
| }); | ||
|
|
||
| test("returns false when the sum of digits is not greater than 16", () => { | ||
| expect(validateCreditCardNumber("1111111111111110")).toEqual(false); | ||
| }); | ||
|
|
||
| test("returns false when the final digit is odd", () => { | ||
| expect(validateCreditCardNumber("6666666666666661")).toEqual(false); | ||
| }); | ||
|
|
||
| test("returns false when the number is not 16 digits long", () => { | ||
| expect(validateCreditCardNumber("1234")).toEqual(false); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,43 @@ | ||
| // A list of previously used passwords that cannot be reused. | ||
| const previousPasswords = ["Password1!", "Welcome2#", "Strong3$"]; | ||
|
|
||
| function passwordValidator(password) { | ||
| return password.length < 5 ? false : true | ||
| } | ||
| // Ensure a password value is provided and that it is a string. | ||
| if (typeof password !== "string") { | ||
| return false; | ||
| } | ||
|
|
||
| // Password must be at least 5 characters long. | ||
| if (password.length < 5) { | ||
| return false; | ||
| } | ||
|
|
||
| // Password must contain at least one uppercase letter. | ||
| if (!/[A-Z]/.test(password)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Password must contain at least one lowercase letter. | ||
| if (!/[a-z]/.test(password)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Password must contain at least one number. | ||
| if (!/[0-9]/.test(password)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Password must contain at least one allowed symbol. | ||
| if (!/[!#$%.*&]/.test(password)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Password must not match any previous password. | ||
| if (previousPasswords.includes(password)) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| module.exports = passwordValidator; | ||
| module.exports = passwordValidator; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
passwordjust not be provided at all?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.
Hi @ykamal,
Good point, thank you.
I updated the function to handle the case where a password is not provided.
Previously the function accessed
password.length, which would throw an error if the value wasundefined.I added an input validation check at the start of the function to ensure the password exists and is a string. If it is missing or not a string, the function now safely returns
false.I also added test cases to cover these scenarios.