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
30 changes: 30 additions & 0 deletions Sprint-3/4-stretch/card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function creditCardValidator(cardNum) {
// Splits the cardNum into an array of characters
const cardNumArray = cardNum.split("");

// Checks if length is 16, otherwise return false
if (cardNumArray.length !== 16) return false;

// checks if all digits are numbers
if (
!cardNumArray.every(
(num) => num.charCodeAt(0) >= 48 && num.charCodeAt(0) <= 57
)
)
return false;

// Checks if there are at least two different digits
const count = cardNumArray.reduce((acc, curr) => {
acc[curr] = acc[curr] ? acc[curr] + 1 : 1;
return acc;
}, {});
if (Object.keys(count).length < 2) return false;

// Checks if the sum of all digits is greater than 16
const sumOfDigits = cardNumArray.reduce((acc, curr) => acc + Number(curr), 0);
if (sumOfDigits <= 16) return false;

//checks if last digit is even
if (cardNumArray[cardNumArray.length - 1] % 2 !== 0) return false;
return true;
}
12 changes: 9 additions & 3 deletions Sprint-3/4-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const previousPasswords = ["5B43n21"];
function passwordValidator(password) {
return password.length < 5 ? false : true
}
if (password.length < 5) return false;
if (previousPasswords.includes(password)) return false;
previousPasswords.push(password);

const rules = [/[A-Z]/, /[a-z]/, /[0-9]/, /[!#$%.*&]/];

return rules.every((rule) => rule.test(password));
}

module.exports = passwordValidator;
module.exports = passwordValidator;
69 changes: 61 additions & 8 deletions Sprint-3/4-stretch/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,64 @@ 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 = "A1b2";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("password is not previously used", () => {
// Arrange
const password = "5B43n21";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("password contains at least one uppercase English letter", () => {
// Arrange
const password = "1a2345";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("password contains at least one uppercase English letter", () => {
// Arrange
const password = "1B2345";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("password contains at least one number(0-9)", () => {
// Arrange
const password = "sdkerjJNGk";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test('password contains at least one of "!", "#", "$", "%", ".", "*", "&"', () => {
// Arrange
const password = "sdkerjJNG23k";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("password meets all the condition for a valid password and passwordValidator returns true", () => {
// Arrange
const password = "sdkerjJNG23k&";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
});
Loading