Repository navigation
London | 26-ITP-Sep | Bartosz Kawiak | Sprint 3 | Stretch Exercises #1743
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,24 @@ | ||
| export function isCardValid(cardNum) { | ||
| if (typeof cardNum !== "number") { | ||
| return false; | ||
| } | ||
| const has16Length = String(cardNum).length === 16; | ||
| const seenDigits = []; | ||
| const isFinalEven = cardNum % 2 === 0; | ||
| let total = 0; | ||
| for (const num of String(cardNum)) { | ||
| if (!seenDigits.includes(num)) { | ||
| seenDigits.push(num); | ||
| } | ||
| const regex = /^\d$/; | ||
| if (!regex.test(num)) { | ||
| return false; | ||
| } | ||
|
Comment on lines
+13
to
+16
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: Since you are using regex, you could also just use regex to check if the whole card number (as a string) contain 16 digits (outside the loop) instead of checking each digit one by one. |
||
| total += Number(num); | ||
| } | ||
| if (has16Length && isFinalEven && total > 16 && seenDigits.length >= 2) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // - Number must be 16 digits, all of them must be numbers. | ||
| // - You must have at least two different digits represented (all of the digits cannot be the same). | ||
| // - The final digit must be even. | ||
| // - The sum of all the digits must be greater than 16. | ||
|
|
||
| import { isCardValid } from "./card-validator.js"; | ||
|
|
||
| test("card number should have 16 digits", () => { | ||
| const cardNumber = 1234567890987654; | ||
| const result = isCardValid(cardNumber); | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
Comment on lines
+8
to
+12
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 checking if the function can correctly return The same also applies to other tests in this script. |
||
|
|
||
| test("card number should have at least two different digits, all of them cannot be the same", () => { | ||
| const cardNumber = 1111111111111111; | ||
| const result = isCardValid(cardNumber); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("final card digit must be even", () => { | ||
| const cardNumber = 1111111111111118; | ||
| const result = isCardValid(cardNumber); | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| test("sum of all the digits must be greater than 16", () => { | ||
| const cardNumber = 2222333344445554; | ||
| const result = isCardValid(cardNumber); | ||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| test("cardNumber must contains only numbers", () => { | ||
| const cardNumber = "1233341234444444"; | ||
| const result = isCardValid(cardNumber); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("cardNumber sum is less than 16, should return false", () => { | ||
| const cardNumber = 1111111111111110; | ||
| const result = isCardValid(cardNumber); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("cardNumber uses invalid characters,should return false", () => { | ||
| const cardNumber = "a92332119c011112"; | ||
| const result = isCardValid(cardNumber); | ||
| expect(result).toEqual(false); | ||
| }); | ||
| test("cardNumber uses only one type of number,should return false", () => { | ||
| const cardNumber = 4444444444444444; | ||
| const result = isCardValid(cardNumber); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("cardNumber has odo final number, should return false", () => { | ||
| const cardNumber = 6666666666666661; | ||
| const result = isCardValid(cardNumber); | ||
| expect(result).toEqual(false); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,32 @@ | ||
| const passwords = []; | ||
| export function isValidPassword(password) { | ||
|
Comment on lines
+1
to
2
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. For higher flexibility, we could design the function to accept |
||
| return password.length >= 5; | ||
| let hasUpper = false; | ||
| let hasLower = false; | ||
| let hasNumbers = false; | ||
| let hasSpecialChars = false; | ||
| const minLength = password.length >= 5; | ||
| const upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
| const lowerLetters = "abcdefghijklmnopqrstuvwxyz"; | ||
| const numbers = "0123456789"; | ||
| const specialChars = "!#$%.*&"; | ||
|
|
||
| for (const char of password) { | ||
| hasUpper = upperLetters.includes(char) ? true : hasUpper; | ||
|
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: hasUpper = upperLetters.includes(char) ? true : hasUpper;is equivalent to hasUpper = hasUpper && upperLetters.includes(char);or if (upperLetters.includes(char)) hasUpper = true;Alternatively, you could use regex to check if |
||
| hasLower = lowerLetters.includes(char) ? true : hasLower; | ||
| hasNumbers = numbers.includes(char) ? true : hasNumbers; | ||
|
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: The term digit refers to a number in the range 0-9. A number may contain any number of digits. |
||
| hasSpecialChars = specialChars.includes(char) ? true : hasSpecialChars; | ||
| } | ||
| if ( | ||
| minLength && | ||
| hasLower && | ||
| hasUpper && | ||
| hasNumbers && | ||
| hasSpecialChars && | ||
| !passwords.includes(password) | ||
| ) { | ||
| passwords.push(password); | ||
|
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. Is "adding the password being checked to a global variable" an expected behavior of this function? |
||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,11 +16,41 @@ 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 = "Aa1!"; | ||
| // Act | ||
| const result = isValidPassword(password); | ||
| // Assert | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("password have at least one uppercase letter(A-Z)", () => { | ||
| const password = "hello123"; | ||
| const result = isValidPassword(password); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("password have at least one lowercase letter(a-z)", () => { | ||
| const password = "A123AA"; | ||
| const result = isValidPassword(password); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("password have at least one number(0-9)", () => { | ||
| const password = "RRRqqq"; | ||
| const result = isValidPassword(password); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
Comment on lines
+39
to
+43
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 function can return For example, the function might return |
||
|
|
||
| test("password have at least one special character(!#$%.*&)", () => { | ||
| const password = "R3ass3"; | ||
| const result = isValidPassword(password); | ||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| test("rejects a previously used password", () => { | ||
| const password = "Hello123!"; | ||
|
|
||
| expect(isValidPassword(password)).toEqual(true); | ||
| expect(isValidPassword(password)).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.
Because of the way JavaScript represents numbers,
isCardValid(9999999999999997)would evaluate totrue.Could you use AI to find out why, and then update your code (both function implementation and tests) to use other data type to represent card number?