Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions sprint-3-stretch-exercises/card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export function isValidCardNumber(cardNumber) {
//The card number must be exactly 16 digits
if (!/^\d{16}$/.test(cardNumber)) {
return false;
}

//It should contains at least two different digits
const uniqueDigits = new Set(cardNumber);
if (uniqueDigits.size < 2) {
return false;
}

//It's last digit must be even
const lastDigit = Number(cardNumber[15]);
if (lastDigit % 2 !== 0) {
return false;
}

//It's sum of all digits must be greater than 16
let sum = 0;
for (const digit of cardNumber) {
sum += Number(digit);
}
if (sum <= 16) {
return false;
}

return true;
}
34 changes: 34 additions & 0 deletions sprint-3-stretch-exercises/card-validator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { isValidCardNumber } from "./card-validator.js";

test("should accept a valid 16-digit card number", () => {
expect(isValidCardNumber("9999777788880000")).toEqual(true);
});

test("should accept another valid 16-digit card number", () => {
expect(isValidCardNumber("6666666666661666")).toEqual(true);
});

test("should reject a card number shorter than 16 digits", () => {
expect(isValidCardNumber("999999999999998")).toEqual(false);
});

test("should reject a card number longer than 16 digits", () => {
expect(isValidCardNumber("99999999999999998")).toEqual(false);
});

test("should reject a card number with a non-digit character", () => {
expect(isValidCardNumber("a92332119c011112")).toEqual(false);
});

test("should reject a card number where all digits are the same", () => {
expect(isValidCardNumber("4444444444444444")).toEqual(false);
});

test("should reject a card number whose digits sum to 16 or less", () => {
expect(isValidCardNumber("1111111111111110")).toEqual(false);
});

test("should reject a card number with an odd final digit", () => {
// same style as a valid number, only last digit is odd
expect(isValidCardNumber("9999777788880001")).toEqual(false);
});
21 changes: 19 additions & 2 deletions sprint-3-stretch-exercises/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
export function isValidPassword(password) {
return password.length >= 5;
export function isValidPassword(password, previousPasswords = []) {
if (password.length < 5) {
return false;
}

const hasUpper = /[A-Z]/.test(password);
const hasLower = /[a-z]/.test(password);
const hasDigit = /[0-9]/.test(password);
const hasSymbol = /[!#$%.*&]/.test(password);

if (!hasUpper || !hasLower || !hasDigit || !hasSymbol) {
return false;
}

if (previousPasswords.includes(password)) {
return false;
}

return true;
}
46 changes: 32 additions & 14 deletions sprint-3-stretch-exercises/password-validator.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
/*
/*
Password Validation

Write a program that should check if a password is valid
and returns a boolean

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.

- Must not be any previous password in the passwords array.
You must breakdown this problem in order to solve it. Find one test case first and get that working
*/
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);
}
);

test("should accept a password that meets all rules", () => {
expect(isValidPassword("Abc1!")).toEqual(true);
});

test("should reject a password shorter than 5 characters", () => {
// only length fails: has upper, lower, digit, symbol
expect(isValidPassword("Ab1!")).toEqual(false);
});

test("should reject a password without an uppercase letter", () => {
expect(isValidPassword("abc1!")).toEqual(false);
});

test("should reject a password without a lowercase letter", () => {
expect(isValidPassword("ABC1!")).toEqual(false);
});

test("should reject a password without a digit", () => {
expect(isValidPassword("Abcde!")).toEqual(false);
});

test("should reject a password without a required symbol", () => {
expect(isValidPassword("Abc12")).toEqual(false);
});

test("should reject a password listed in previousPasswords", () => {
// meets all other rules; fails only because it is in the list
expect(isValidPassword("Abc1!", ["Abc1!"])).toEqual(false);
});
Loading