-
-
Notifications
You must be signed in to change notification settings - Fork 391
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
4fa3ff3
66c5040
4ff2ef1
817489d
27ea3d0
b2ba40c
071f6b3
327cc0a
454e73a
be14793
fe8cd1d
3c2d06c
04e97b7
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,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; | ||
| 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); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,24 @@ | ||
| function passwordValidator(password) { | ||
| return password.length < 5 ? false : true | ||
| /* 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. | ||
| */ | ||
| const passwords = ["pa$$w0rd", "Qwerty1#", "Adm1n2#", "$3cr4t"]; | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
| if ( | ||
| password.length < 5 || | ||
| !/[A-Z]/.test(password) || | ||
| !/[a-z]/.test(password) || | ||
| !/[0-9]/.test(password) || | ||
| !/[!#\$%\.\*&]/.test(password) || | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
| passwords.includes(password) | ||
| ) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| module.exports = passwordValidator; | ||
| module.exports = passwordValidator; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,11 +16,100 @@ You must breakdown this problem in order to solve it. Find one test case first a | |
| */ | ||
| 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); | ||
| } | ||
| ); | ||
| // Arrange | ||
| const password = "12345cA!"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| test("password with less than 5 characters is invalid", () => { | ||
| // Arrange | ||
| const password = "1Aa%"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("password has at least one English uppercase letter (A-Z)", () => { | ||
| // Arrange | ||
| const password = "1234bA$"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| test("password with no uppercase letters is invalid", () => { | ||
| // Arrange | ||
| const password = "1234ab$"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("password has at least one English lowercase letter (a-z)", () => { | ||
| // Arrange | ||
| const password = "1234Aa%"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
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. Note:
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. awesome, thanks. |
||
|
|
||
| test("password with no lowercase letters is invalid", () => { | ||
| // Arrange | ||
| const password = "1234AB$"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("password has at least one number (0-9)", () => { | ||
| // Arrange | ||
| const password = "1234Aa&"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| test("password with no numbers is invalid", () => { | ||
| // Arrange | ||
| const password = "passWord!"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("password has at least one non-alphanumeric symbol: (!, #, $, %, ., *, &)", () => { | ||
| // Arrange | ||
| const password = "1234aA#"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| test("must not be any previous password in the passwords array.", () => { | ||
| // Arrange | ||
| const password = "pas$W0rd"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| test("previous passwords in the passwords array are invalid", () => { | ||
| // Arrange | ||
| const password = "Qwerty1#"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
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