-
-
Notifications
You must be signed in to change notification settings - Fork 336
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
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,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. | ||
|
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. writing down these rules as comments in code is very helpful for making sure you complete all of them and also helpful for debugging in plain english. good job |
||
| 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; | ||
| } | ||
|
Comment on lines
+21
to
+28
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. while this works, it is slightly inefficient as it requires you to loop through the entire list of digits before saying true/false even if the first 4 3 digits are more than 16 for example. Can you think of a way to make this more efficient and return earlier? |
||
|
|
||
| return true; | ||
| } | ||
|
|
||
| module.exports = validateCreditCardNumber; | ||
| 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); | ||
| }); |
| 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) { | ||
|
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. Could
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. Hi @ykamal, Good point, thank you. I updated the function to handle the case where a password is not provided. 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 I also added test cases to cover these scenarios. |
||
| 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; | ||
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.
this is more of an advanced thing, but as a general rule if you're implementing a function that returns a boolean (true/false) its best to start the function name with "is" or "has" to make it clear to a reader that it returns a boolean. So this one could be called "isCreditCardNumberValid" for example