Skip to content

Commit 4e34493

Browse files
written test jests for password validator to check based on the given requirements
1 parent 4f63a8d commit 4e34493

3 files changed

Lines changed: 81 additions & 3 deletions

File tree

Sprint-3/4-stretch/find.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ 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+
// During the call to find(str, char): index starts at 0 and on each loop iteration, the function checks if str[index] is equal to char. if it is not, index is incremented by 1 (index++) and the loop continues until either a match is found or index reaches str.length. If a match is found, the function returns the current value of index. If no match is found after checking all characters, the function returns -1.
24+
// C O D E Y O "U" R F U T U R E
25+
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 => MATCH FOUND --> return index = 7
26+
2327
// b) What is the if statement used to check
28+
// The if statement checks if str[index] is equal to char. This implies whether the current character in the string matches the character we are searching for. If it does, the function returns the current index. If not, the loop continues to the next character.
2429
// c) Why is index++ being used?
30+
// index++ is used to increment the index variable by 1 after each loop iteration and this is done to move to the next character in the string for comparison with char. This enables the function to check each character in the string sequentially until a match is found or the end of the string is reached.
2531
// d) What is the condition index < str.length used for?
32+
// The condition index < str.length is used to ensure that the loop continues to run as long as the index is less than the length of the string. This prevents the function from trying to run indefinitely and ensures that it only checks valid indices within the bonds of a given string. Therefore, the loop will terminate when index reaches str.length, indicating that all characters have been checked without finding a match.
Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
function passwordValidator(password) {
2-
return password.length < 5 ? false : true
2+
// must be at least 5 characters long
3+
if (password.length < 5) {
4+
return false;
5+
}
6+
// must contain at least one lowercase letter
7+
const hasLowercase = /[a-z]/.test(password);
8+
// must contain at least one uppercase letter
9+
const hasUppercase = /[A-Z]/.test(password);
10+
// must contain at least one digit
11+
const hasDigit = /[0-9]/.test(password);
12+
// must contain at least one special character
13+
const hasSpecialChar = /[!@#$%^&*()\-+]/.test(password);
14+
// must not contain any spaces
15+
const hasNoSpaces = !/\s/.test(password);
16+
// must not be any previous password in the passwords array
17+
const isNewPassword = !previouspasswords.includes(password);
18+
return (
19+
hasLowercase &&
20+
hasUppercase &&
21+
hasDigit &&
22+
hasSpecialChar &&
23+
hasNoSpaces &&
24+
isNewPassword
25+
);
326
}
427

5-
6-
module.exports = passwordValidator;
28+
module.exports = passwordValidator;

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ To be valid, a password must:
1111
- Have at least one number (0-9)
1212
- Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&")
1313
- Must not be any previous password in the passwords array.
14+
-Must not contain any spaces.
1415
1516
You must breakdown this problem in order to solve it. Find one test case first and get that working
1617
*/
@@ -23,4 +24,52 @@ test("password has at least 5 characters", () => {
2324
// Assert
2425
expect(result).toEqual(true);
2526
}
27+
test("password has at least one English uppercase letter (A-Z)", () => {
28+
// Arrange
29+
const password = "1A345";
30+
// Act
31+
const result = isValidPassword(password);
32+
// Assert
33+
expect(result).toEqual(true);
34+
}
35+
test("password has at least one English lowercase letter (a-z)", () => {
36+
// Arrange
37+
const password = "1a345";
38+
// Act
39+
const result = isValidPassword(password);
40+
// Assert
41+
expect(result).toEqual(true);
42+
}
43+
test("password has at least one number (0-9)", () => {
44+
// Arrange
45+
const password = "Abcd5";
46+
// Act
47+
const result = isValidPassword(password);
48+
// Assert
49+
expect(result).toEqual(true);
50+
}
51+
test("password has at least one special character", () => {
52+
// Arrange
53+
const password = "1234!";
54+
// Act
55+
const result = isValidPassword(password);
56+
// Assert
57+
expect(result).toEqual(true);
58+
}
59+
test("password has no spaces", () => {
60+
// Arrange
61+
const password = "Abc 5$";
62+
// Act
63+
const result = isValidPassword(password);
64+
// Assert
65+
expect(result).toEqual(false);
66+
}
67+
test("password is not a previous password", () => {
68+
// Arrange
69+
const password = "12345";
70+
// Act
71+
const result = isValidPassword(password);
72+
// Assert
73+
expect(result).toEqual(false);
74+
}
2675
);

0 commit comments

Comments
 (0)