Repository navigation
London | 26-ITP-May | Russom Gebremeskel | Sprint 3 | Coursework Stretch #1746
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
301ea63
2501561
63e477b
a8e1b2d
9e2cef8
2ba2e34
06d5f91
3dc6da6
1e6cb55
61b2ce7
0407869
06d3f9a
598f744
e6fde3b
c2f743c
e7ff9c7
751fb78
4707f5a
f504687
d9197c4
9494c3d
582699c
b806b41
fcf27e7
04f544f
4815e87
9dc9e1f
10b0d0f
cccf002
ed2729e
4fa3e6c
ea5fa0f
6102a89
717584d
fed8ddd
bb815ef
c8fc9dd
3a565e9
c80ed87
38c28b3
f12c635
48b8dae
d5091de
fa07e40
71c6ea1
c12bed8
1b777bf
5d29d04
63905f0
be0c56e
f05ab1d
d6e7d06
2431fdf
7421372
51a7234
7465a37
b5ed6ae
d363d13
bd5de48
616bbbf
2de2d40
05f3e6e
ba2ebd0
52f6943
f950077
0c5efc1
3e016dc
8cde26f
db81678
0aede1c
9140665
b627dd7
3df0769
9a77853
62a181f
a5484ec
6783e06
38850b8
67f3a50
1a97e33
60849f7
9bb4e9f
4bb8fb0
f051b49
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,32 @@ | ||
| export function creditCardValidator(cardNumber) { | ||
| // rejects the number if itn's exactly 16 characters | ||
| if (cardNumber.length !== 16) { | ||
| return false; | ||
| } | ||
| // the last digit must be even, so reject it if dividing by 2 leaves a reminder | ||
| if (cardNumber.slice(-1) % 2 !== 0) { | ||
| return false; | ||
| } | ||
|
|
||
| let hasDifferentDigit = false; | ||
| let digitSum = 0; | ||
|
|
||
| for (const digit of cardNumber) { | ||
| // every character must be a digit from 0 to 9 | ||
| if (digit < "0" || digit > "9") { | ||
| return false; | ||
| } | ||
| digitSum += Number(digit); | ||
|
|
||
| // if any digit differs from the first one, at least two different digits are used | ||
| if (digit !== cardNumber[0]) { | ||
| hasDifferentDigit = true; | ||
| } | ||
| } | ||
|
|
||
| // needs two different digits and a digit sum greater than 16 | ||
| if (!hasDifferentDigit || digitSum <= 16) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Case 1 valid card numbers. | ||
| import { creditCardValidator } from "./credit-card-validator.js"; | ||
| test("should return true for a valid card number", () => { | ||
| const number = "2222222222222224"; | ||
| const result = creditCardValidator(number); | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| // Case 2 number must be 16 digits. | ||
| test("should return false when the numbers has fewer than 16 digits", () => { | ||
| const number = "123"; | ||
| const result = creditCardValidator(number); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 3 numbers must be whole numbers. | ||
| test("should return false when the number contains a letter", () => { | ||
| const number = "22222222222222b4"; | ||
| const result = creditCardValidator(number); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 4 numbers must have at least two different digits represented. | ||
| test("should return false when all digits are the same.", () => { | ||
| const number = "2222222222222222"; | ||
| const result = creditCardValidator(number); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 5 the final digit of the number must be even. | ||
| test("should return false when the last digit is odd", () => { | ||
| const number = "2222222222222223"; | ||
| const result = creditCardValidator(number); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 6 the sum of all the digits must be greater than 16. | ||
| test("should return false when the sum of digits is 16 or less", () => { | ||
| const number = "1111111111110002"; | ||
| const result = creditCardValidator(number); | ||
| expect(result).toEqual(false); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| export function isValidPassword(password) { | ||
| return password.length >= 5; | ||
| export function isValidPassword(password, previousPasswords = []) { | ||
| if (previousPasswords.includes(password)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (password.length < 5) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!/[A-Z]/.test(password)) return false; | ||
| if (!/[a-z]/.test(password)) return false; | ||
| if (!/[0-9]/.test(password)) return false; | ||
| if (!/[!#$%.*&]/.test(password)) return false; | ||
|
|
||
| return true; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,11 +16,62 @@ You must breakdown this problem in order to solve it. Find one test case first a | |
| */ | ||
| import { isValidPassword } from "./password-validator.js"; | ||
| test("password has at least 5 characters", () => { | ||
| // Arrange | ||
| const password = "12345"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(true); | ||
| } | ||
| ); | ||
| // Arrange | ||
| const password = "Az9!"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| // Have at least one English uppercase letter (A-Z) | ||
| test("password has at least uppercase letter", () => { | ||
| const password = "1az9!"; | ||
| const result = isValidPassword(password); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| // Have at least one English lowercase letter (a-z) | ||
| test("password has at least lowercase letter", () => { | ||
| const password = "1AZ9!"; | ||
| const result = isValidPassword(password); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| // Have at least one number (0-9) | ||
| test("password has at least one number (0-9)", () => { | ||
| const password = "Aazc!"; | ||
| const result = isValidPassword(password); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| // Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&") | ||
| test("password has at least non-alphanumeric symbols", () => { | ||
| const password = "1Az99"; | ||
| const result = isValidPassword(password); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| // Must not be any previous password in the passwords array. | ||
| test("password must not be any previous password in the passwords array", () => { | ||
| const password = "1Az9!"; | ||
|
|
||
| const previousPassword1 = ["1Az9!", "1HBz2!"]; | ||
| const result1 = isValidPassword(password, previousPassword1); | ||
| expect(result1).toEqual(false); | ||
|
|
||
| const previousPassword2 = ["1HBz2!", "1Az9!"]; | ||
| const result2 = isValidPassword(password, previousPassword2); | ||
| expect(result2).toEqual(false); | ||
|
|
||
| const previousPassword3 = ["1Az8!", "1HBz2!"]; | ||
| const result3 = isValidPassword(password, previousPassword3); | ||
| expect(result3).toEqual(true); | ||
| }); | ||
|
Comment on lines
+56
to
+70
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. In addition to testing if the function could return We could include multiple To make the test even more comprehensive, we could also test if the function could work correctly when the matching previous password appears as the first/last element in the array.
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. Test cases added when previous password doesn't match and when swapping previous password element position in the array. |
||
|
|
||
| // Valid password returns true. | ||
| test("valid password returns true", () => { | ||
| const password = "1Az9!"; | ||
| const result = isValidPassword(password); | ||
| expect(result).toEqual(true); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.