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
23 changes: 23 additions & 0 deletions Sprint-3/4-stretch/card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 >= "0" && num <= "9")) return false;

// Checks if there are at least two different digits
const count = new Set(cardNumArray);
if (count.size < 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;
}
console.log(creditCardValidator("1111111111111111"));
15 changes: 11 additions & 4 deletions Sprint-3/4-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
function passwordValidator(password) {
return password.length < 5 ? false : true
}
function passwordValidator(password, previousPasswords = []) {
if (!Array.isArray(previousPasswords)) return false;

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;
80 changes: 71 additions & 9 deletions Sprint-3/4-stretch/password-validator.test.js
Copy link
Contributor

@cjyuan cjyuan Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make the test descriptions more detailed and descriptive? That way, if a test fails, the person implementing the function can quickly understand what went wrong and why.

Right now, for example, "password contains at least one number (0-9)" does not quite indicate what the test is testing. (Did you mean to say "not containing")

Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,74 @@ To be valid, a password must:
You must breakdown this problem in order to solve it. Find one test case first and get that working
*/
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);
}
);
test("should return false if password has fewer than 5 characters", () => {
// Arrange
const password = "A1b&";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("should return false if password was previously used", () => {
// Arrange
const password = "5B43n21!";
// Act
const result = isValidPassword(password, "5B43n21!");
Copy link
Contributor

@cjyuan cjyuan Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected this test to fail because the second parameter isn't an array of strings.

I didn't realise string also has a method .includes() and your function manages to pass this test.

However, other implementations that expect the second parameter to be an array may fail this test unexpectedly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate all the comments.
I missed that it should be an array and it's surprising the code is working.
But if function explicitly checks for an array then it'll fail, so I'll add the check to the function and change the test as well.
Thanks.

// Assert
expect(result).toEqual(false);
});

test("should return false if password does not contain an uppercase English letter", () => {
// Arrange
const password = "1a2345&";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("should return false if password does not contain an lowercase English letter", () => {
// Arrange
const password = "1B2345%";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("should return false if password has no digit", () => {
// Arrange
const password = "se!rjJN%Gk";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test('should return false if password has no special characters including "!", "#", "$", "%", ".", "*", "&"', () => {
// Arrange
const password = "sdkerjJNG23k";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("should return true if password meets all validation rules", () => {
// Arrange
const password = "sdkerj!JNG23k&";
// Act
const result = isValidPassword(password, ["se!rjJN%G6k", "1B2h345%a"]);
// Assert
expect(result).toEqual(true);
});

test("should return false if previousPassword is not an array", () => {
// Arrange
const password = "sdkerj!JNG23k&";
// Act
const result = isValidPassword(password, "1B2h345%a");
// Assert
expect(result).toEqual(false);
});