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
1 change: 1 addition & 0 deletions Project-CLI-Treasure-Hunt
Submodule Project-CLI-Treasure-Hunt added at 6668c4
37 changes: 36 additions & 1 deletion Sprint-3/3-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
// function isLongEnough(password) {
// return password.length >= 5;
// }
// function isUpperCase(password) {
// return /[A-Z]/.test(password);
// }

// function isLowerCase(password) {
// return /[a-z]/.test(password);
// }

// function hasNumber(password) {
// return /[0-9]/.test(password);
// }

// function hasSpecialChar(password) {
// return /[!#$%.*&]/.test(password);
// }
// function passwordValidator(password) {

// if (isLongEnough(password) && isUpperCase(password) && isLowerCase(password) && hasNumber(password) && hasSpecialChar(password))
// return true
// else
// return false;
// }

/////////////////////////////////////////////////////////////////////

function passwordValidator(password) {
return password.length < 5 ? false : true
const isLongEnough = password.length >= 5;
const hasUpper = /[A-Z]/.test(password);
const hasLower = /[a-z]/.test(password);
const hasNumber = /[0-9]/.test(password);
Copy link
Contributor

@cjyuan cjyuan Nov 21, 2025

Choose a reason for hiding this comment

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

Can consider name it hasDigit. '0', ..., '9' are called digits.

const hasSpecial = /[!#$%.*&]/.test(password);

return isLongEnough && hasUpper && hasLower && hasNumber && hasSpecial;
}

console.log(passwordValidator("Password123!"));

module.exports = passwordValidator;
31 changes: 26 additions & 5 deletions Sprint-3/3-stretch/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,32 @@ 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 password = "12345Aa!";
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
}
);
Comment on lines 18 to -26
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also consider testing boundary cases:

  • Valid password with 5 characters
  • A 4-character password containing all required characters.

);
test("password missing uppercase letter", () => {
const password = "12345aa!";
const result = isValidPassword(password);
expect(result).toEqual(false);
}
);
test("password missing lowercase letter", () => {
const password = "12345AA!";
const result = isValidPassword(password);
expect(result).toEqual(false);
}
);
test("password missing number", () => {
const password = "Abcde!";
const result = isValidPassword(password);
expect(result).toEqual(false);
}
);
test("password missing special character", () => {
const password = "12345Aa";
const result = isValidPassword(password);
expect(result).toEqual(false);
}
Comment on lines +24 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to also specify in the test description

  • the expected behavior of the function ("should return false when ..."), or
  • indicate the test is for invalid passwords

);