generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 261
NW | 25-ITP-Sep | TzeMing Ho | Sprint 3 | coursework/sprint-3-stretch #714
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
TzeMingHo
wants to merge
16
commits into
CodeYourFuture:main
Choose a base branch
from
TzeMingHo:coursework/sprint-3-stretch
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
16 commits
Select commit
Hold shift + click to select a range
9d43b8b
npm installed
TzeMingHo b0265ba
answer part a in find.js
TzeMingHo f1ac2fe
answered part b to d in find.js
TzeMingHo 78b6e92
writing conditions and tests for password validation
TzeMingHo 1b11c5a
added a break line in test file
TzeMingHo 596bf65
created credit card validator file and its tests file
TzeMingHo bfae2f8
removing all space and - from the string input
TzeMingHo 4eb2d5b
created tests and function for credit card validation
TzeMingHo 9c8e884
restore package-lock.json from origin/main
TzeMingHo f0d7b15
adding a variable for card valid length
TzeMingHo d5d16fa
adding tests for card number length validation and only digit
TzeMingHo 5a7757f
updating return statement in password-validator.js
TzeMingHo 9447d08
updated password to validaPassword in tests
TzeMingHo 791b52a
adding tests for false cases
TzeMingHo 70f9400
added a test for only 16 digits with spaces or operators, updated var…
TzeMingHo c1efb3f
moving previous passwords to test file
TzeMingHo 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,41 @@ | ||
| function creditCardValidator(cardNumber) { | ||
| // declare the length of valid card number | ||
| const VALID_LENGTH = 16; | ||
| const minimumSum = 16; | ||
|
|
||
| // Remove all - and spaces from the input | ||
| const sanitized = cardNumber.replace(/[-\s]/g, ""); | ||
|
|
||
| //check if the length of the sanitized input is 16 | ||
| if (sanitized.length !== VALID_LENGTH) { | ||
| return false; | ||
| } | ||
| // check if the sanitized input contains only digits | ||
| if (!new RegExp(`^\\d{${VALID_LENGTH}}$`).test(sanitized)) { | ||
| return false; | ||
| } | ||
|
|
||
| // check if there ara at least two different digits | ||
| const uniqueDigits = new Set(sanitized); | ||
| if (uniqueDigits.size < 2) { | ||
| return false; | ||
| } | ||
|
|
||
| // check if the last digit is even | ||
| const lastDigit = Number(sanitized[sanitized.length - 1]); | ||
| if (lastDigit % 2 !== 0) { | ||
| return false; | ||
| } | ||
|
|
||
| // check if the sum of all digits is greater than 16 | ||
| const sum = sanitized | ||
| .split("") | ||
| .reduce((total, digit) => total + Number(digit), 0); | ||
| if (sum <= minimumSum) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| module.exports = creditCardValidator; |
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,48 @@ | ||
| const creditCardValidator = require("./creditCard-validator"); | ||
|
|
||
| describe("creditCardValidator", () => { | ||
| test("should return true for a 16 digit long card number", () => { | ||
| expect(creditCardValidator("1234-5678-9012-3456")).toBe(true); | ||
| expect(creditCardValidator("1234 5678 9012 3456")).toBe(true); | ||
| }); | ||
|
|
||
| test("should return false if the card number is not digit only", () => { | ||
| expect(creditCardValidator("1234-5678-abcd-3456")).toBe(false); | ||
| expect(creditCardValidator("1234 5678 9012 3456")).toBe(true); | ||
| }); | ||
|
|
||
| test("should return false if the card number does not contain at least two different digits", () => { | ||
| expect(creditCardValidator("1111-1111-1111-1111")).toBe(false); | ||
| expect(creditCardValidator("1111 2222 2222 2222")).toBe(true); | ||
| }); | ||
|
|
||
| test("should return false if the card number does not end with an even digit", () => { | ||
| expect(creditCardValidator("1234-5678-9012-3457")).toBe(false); | ||
| expect(creditCardValidator("1234 5678 9012 3456")).toBe(true); | ||
| }); | ||
|
|
||
| test("should return false if the sum of all the digits is not greater than 16", () => { | ||
| expect(creditCardValidator("0000-0000-0000-0000")).toBe(false); | ||
| expect(creditCardValidator("1111 1111 1111 1111")).toBe(false); | ||
| expect(creditCardValidator("1234 5678 9012 3456")).toBe(true); | ||
| }); | ||
|
|
||
| // test for less than 16 digits | ||
| test("should return false for a card number with less than 16 digits", () => { | ||
| expect(creditCardValidator("123456789012345")).toBe(false); | ||
| expect(creditCardValidator("1234-5678-9012-345")).toBe(false); | ||
| }); | ||
|
|
||
| // test for more than 16 digits | ||
| test("should return false for a card number with more than 16 digits", () => { | ||
| expect(creditCardValidator("12345678901234567")).toBe(false); | ||
| expect(creditCardValidator("1234-5678-9012-34567")).toBe(false); | ||
| }); | ||
|
|
||
| // test for only 16 digits without spaces or dashes | ||
| test("should return true for a card number with 16 digits without spaces or dashes", () => { | ||
| expect(creditCardValidator("1234567890123456")).toBe(true); | ||
| expect(creditCardValidator("0000000000000000")).toBe(false); | ||
| expect(creditCardValidator("1111111111111111")).toBe(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
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,30 @@ | ||
| function passwordValidator(password) { | ||
| return password.length < 5 ? false : true | ||
| } | ||
| function isValidPassword(password, previousPasswords) { | ||
| const lengthCondition = password.length >= 5; | ||
| console.log(`length condition: ${lengthCondition}`); | ||
|
|
||
| const uppercaseCondition = /[A-Z]/.test(password); | ||
| console.log(`upper case condition: ${uppercaseCondition}`); | ||
|
|
||
| const lowercaseCondition = /[a-z]/.test(password); | ||
| console.log(`lower case condition: ${lowercaseCondition}`); | ||
|
|
||
| const numberCondition = /[0-9]/.test(password); | ||
| console.log(`number condition: ${numberCondition}`); | ||
|
|
||
| const symbolCondition = /[!#$%.*&]/.test(password); | ||
| console.log(`symbol condition: ${symbolCondition}`); | ||
|
|
||
| const notInPreviousPasswords = !previousPasswords.includes(password); | ||
| console.log(`previous password condition: ${notInPreviousPasswords}`); | ||
|
|
||
| return ( | ||
| lengthCondition && | ||
| uppercaseCondition && | ||
| lowercaseCondition && | ||
| numberCondition && | ||
| symbolCondition && | ||
| notInPreviousPasswords | ||
| ); | ||
| } | ||
|
|
||
| module.exports = passwordValidator; | ||
| module.exports = isValidPassword; |
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
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.
Your tests all currently assume there are extra characters in the credit card number - is
123456789012345valid? You may want to test that kind of thing too, to make sure your implementation doesn't assume there are separators.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.
You are right. I should have included tests for card numbers that are more or fewer than 16 digits. As you suggested, I created one test for the case of fewer and another for more.
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.
These extra tests look good, but aren't quite getting at the problem I was pointing at - can you add a test for a 16-digit credit card number which is only numbers (i.e. no spaces or
-s)?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.
Yeah. I included a test for only 16 digits without spaces or operators.