-
-
Notifications
You must be signed in to change notification settings - Fork 390
London | 26-ITP-May | Tomislav Dukez | Sprint 3 | Stretch #1454
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
tomdu3
wants to merge
13
commits into
CodeYourFuture:main
Choose a base branch
from
tomdu3:coursework/sprint-3/4-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
13 commits
Select commit
Hold shift + click to select a range
4fa3ff3
add explanatory comments to task find.js
tomdu3 66c5040
add check for pass length
tomdu3 4ff2ef1
add check pass for upper case letter
tomdu3 817489d
add check pass for lower case letter
tomdu3 27ea3d0
add check pass for number
tomdu3 b2ba40c
add check pass for symbols
tomdu3 071f6b3
add validation and tests historical passwords and more other tests
tomdu3 327cc0a
add card-validator code and tests
tomdu3 454e73a
fix answer values of index
tomdu3 be14793
fix improve passwordValidator function
tomdu3 fe8cd1d
fix regex expression for special chars
tomdu3 3c2d06c
fix remove additonal test
tomdu3 04e97b7
fix remove redundant valid tests
tomdu3 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,38 @@ | ||
| function validateCreditCardNumber(cardNumber) { | ||
| // Check if the card number is a positive integer. | ||
| if (typeof cardNumber !== "number" || cardNumber < 0) { | ||
| return false; | ||
| } | ||
|
|
||
| const cardNumberString = String(cardNumber); | ||
| // Check if the card number has at least two different digits | ||
| const uniqueDigits = new Set(cardNumberString); | ||
|
|
||
| if (uniqueDigits.size < 2) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if the card number has exactly 16 digits. | ||
| if (cardNumberString.length !== 16) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if the last digit is even | ||
| if (Number(cardNumberString.slice(-1)) % 2 !== 0) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if the sum of all the digits is greater than 16 | ||
| const sumOfAllDigits = Array.from(cardNumberString).reduce( | ||
| (sum, digit) => sum + Number(digit), | ||
| 0 | ||
| ); | ||
|
|
||
| if (sumOfAllDigits <= 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,91 @@ | ||
| const validateCreditCardNumber = require("./card-validator"); | ||
|
|
||
| test("Number must be 16 digits, all of them must be numbers.", () => { | ||
| // Arrange | ||
| const cardNumber = 1234567890123456; | ||
| // Act | ||
| const result = validateCreditCardNumber(cardNumber); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| test("Credit card number must have at least two different digits.", () => { | ||
| // Arrange | ||
| const cardNumber = 6262826262628262; | ||
| // Act | ||
| const result = validateCreditCardNumber(cardNumber); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| test("Credit card with only one repeating digit is invalid.", () => { | ||
| // Arrange | ||
| const cardNumber = 8888888888888888; | ||
| // Act | ||
| const result = validateCreditCardNumber(cardNumber); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("Credit card number cannot have an odd last digit.", () => { | ||
| // Arrange | ||
| const cardNumber = 1234567890123457; | ||
| // Act | ||
| const result = validateCreditCardNumber(cardNumber); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("Credit card number sum of all digits cannot be less than or equal to 16.", () => { | ||
| // Arrange | ||
| const cardNumber = 1000000000000000; | ||
| // Act | ||
| const result = validateCreditCardNumber(cardNumber); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("Credit card number sum of all digits must be greater than 16.", () => { | ||
| // Arrange | ||
| const cardNumber = 1234567890123452; | ||
| // Act | ||
| const result = validateCreditCardNumber(cardNumber); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| test("Credit card number must not be negative.", () => { | ||
| // Arrange | ||
| const cardNumber = -1234567890123456; | ||
| // Act | ||
| const result = validateCreditCardNumber(cardNumber); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("Credit card number must be a number.", () => { | ||
| // Arrange | ||
| const cardNumber = "1234567890123456"; | ||
| // Act | ||
| const result = validateCreditCardNumber(cardNumber); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("Credit card number must have exactly 16 digits.", () => { | ||
| // Arrange | ||
| const cardNumber = 212222222212222; | ||
| // Act | ||
| const result = validateCreditCardNumber(cardNumber); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("Credit card number cannot have less or more than 16 digits.", () => { | ||
| // Arrange | ||
| const cardNumber = 212222222212222; | ||
| // Act | ||
| const result = validateCreditCardNumber(cardNumber); | ||
| // Assert | ||
| expect(result).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 |
|---|---|---|
|
|
@@ -20,6 +20,19 @@ console.log(find("code your future", "z")); | |
| // Pay particular attention to the following: | ||
|
|
||
| // a) How the index variable updates during the call to find | ||
| // The index variable starts at 0 and increments by 1 in each iteration of the while loop. | ||
| // In case of the call find("code your future", "u"), the index variable updates as follows: | ||
| // 0, 1, 2, 3, 4, 5, 6, 7 - when it stops, because the character at index 7 is "u" | ||
| // In case of the call find("code your future", "z"), the index variable updates as follows: | ||
| // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 - when it stops, because the condition index < str.length is no longer true | ||
| // the and because the loop has arrived to the end of the string without finding "z". | ||
|
Comment on lines
+26
to
+28
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.
The last value of |
||
|
|
||
| // b) What is the if statement used to check | ||
| // The if statement is used to check if the character at the current index is equal to the character we are looking for. | ||
|
|
||
| // c) Why is index++ being used? | ||
| // index++ is incrementing the index by 1 in each iteration of the while loop. this is how the index is being moved from 0 to the end of the string, or the index where the character is found. | ||
|
|
||
| // d) What is the condition index < str.length used for? | ||
| // It gives the limit to the looping, that is, the index must be less than the length of the string, and in case the index is equal or greater than the string length, the loop will stop. | ||
| // If this condition wasn't given, the loop would continue to run indefinitely. | ||
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,26 @@ | ||
| function passwordValidator(password) { | ||
| return password.length < 5 ? false : true | ||
| function passwordValidator( | ||
| password, | ||
| usedPasswords = ["pa$$w0rd", "Qwerty1#", "Adm1n2#", "$3cr4t"] | ||
| ) { | ||
| /* To be valid, a password must: | ||
| - Have at least 5 characters. | ||
| - Have at least one English uppercase letter (A-Z) | ||
| - Have at least one English lowercase letter (a-z) | ||
| - Have at least one number (0-9) | ||
| - Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&") | ||
| - Must not be any previous password in the passwords array. | ||
| */ | ||
| if ( | ||
| password.length < 5 || | ||
| !/[A-Z]/.test(password) || | ||
| !/[a-z]/.test(password) || | ||
| !/[0-9]/.test(password) || | ||
| !/[!#$%.*&]/.test(password) || | ||
| usedPasswords.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.
Note:
To check if a value is in integer,
Number.isInteger()is better.Credit card numbers typically have 16 digits, but not all 16-digit integers can be safely represented as a number in JS -- many numbers larger than
Number.MAX_SAFE_INTEGERcannot be represented properly. For example,9007199254740993. Normally, card numbers are represented as strings.No change required.
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.
cool observation. thanks