Skip to content

Commit 5c1ed39

Browse files
committed
Implement full password validator and corresponding Jest tests
1 parent 8f3d6cf commit 5c1ed39

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

Sprint-3/3-stretch/find.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ console.log(find("code your future", "z"));
2020
// Pay particular attention to the following:
2121

2222
// a) How the index variable updates during the call to find
23+
24+
// Starts at 0, increments by 1 each iteration (index++)
25+
// Moves through each character position: 0 → 1 → 2 → 3 → etc.
26+
2327
// b) What is the if statement used to check
28+
29+
// Checks if the character at the current position (str[index]) matches the character we're searching for (char)
30+
2431
// c) Why is index++ being used?
32+
33+
// To move to the next character position in the string for the next iteration
34+
// Without it, we'd have an infinite loop stuck at the same position
35+
2536
// d) What is the condition index < str.length used for?
37+
38+
// Ensures we don't go beyond the string length
39+
// Prevents accessing invalid memory locations
40+
// Stops the loop when we've checked all characters
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
function passwordValidator(password) {
2-
return password.length < 5 ? false : true
3-
}
2+
// Check length (at least 5 characters)
3+
if (password.length < 5) {
4+
return false;
5+
}
6+
7+
// Check for at least one uppercase letter (A-Z)
8+
if (!/[A-Z]/.test(password)) {
9+
return false;
10+
}
11+
12+
// Check for at least one lowercase letter (a-z)
13+
if (!/[a-z]/.test(password)) {
14+
return false;
15+
}
416

17+
// Check for at least one number (0-9)
18+
if (!/[0-9]/.test(password)) {
19+
return false;
20+
}
21+
22+
// Check for at least one special symbol: !, #, $, .
23+
if (!/[!#$.]/.test(password)) {
24+
return false;
25+
}
26+
27+
// If all checks pass, return true
28+
return true;
29+
}
530

631
module.exports = passwordValidator;

Sprint-3/3-stretch/password-validator.test.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,38 @@ To be valid, a password must:
1414
1515
You must breakdown this problem in order to solve it. Find one test case first and get that working
1616
*/
17+
1718
const isValidPassword = require("./password-validator");
19+
1820
test("password has at least 5 characters", () => {
19-
// Arrange
20-
const password = "12345";
21-
// Act
22-
const result = isValidPassword(password);
23-
// Assert
24-
expect(result).toEqual(true);
25-
}
26-
);
21+
// Arrange
22+
const password = "Abc1!";
23+
// Act
24+
const result = isValidPassword(password);
25+
// Assert
26+
expect(result).toEqual(true);
27+
});
28+
29+
test("password with less than 5 characters returns false", () => {
30+
expect(isValidPassword("Ab1!")).toEqual(false);
31+
});
32+
33+
test("password without uppercase returns false", () => {
34+
expect(isValidPassword("abc1!")).toEqual(false);
35+
});
36+
37+
test("password without lowercase returns false", () => {
38+
expect(isValidPassword("ABC1!")).toEqual(false);
39+
});
40+
41+
test("password without number returns false", () => {
42+
expect(isValidPassword("Abcd!")).toEqual(false);
43+
});
44+
45+
test("password without special symbol returns false", () => {
46+
expect(isValidPassword("Abcd1")).toEqual(false);
47+
});
48+
49+
test("valid password with all requirements returns true", () => {
50+
expect(isValidPassword("Abc1!")).toEqual(true);
51+
});

0 commit comments

Comments
 (0)