This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy path8-password-validator.js
More file actions
116 lines (104 loc) · 3.12 KB
/
8-password-validator.js
File metadata and controls
116 lines (104 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Password Validation
Write a program that should check if each password in an array
contains a valid password (see below for password criteria) and return a
new array with true or false booleans for whether that password was valid or not.
To be valid, a password must:
- Have at least 5 characters.
- Have at least one English uppercase letter (A-Z)
- Have at least one English lowercase letter (a-z)
- Have at least one number (0-9)
- Have at least one non-alphanumeric symbol ("!", "#", "$", "%", ".", "*", "&")
- Must not be any previous password in the passwords array.
We have supplied functions which will help you with some of these checks.
Example 1:
PreviousPassword = ["fhD8!yrjj", "ttkTu.wer3", "dvyyeyY!5", "qwbfj76%", "tytT3729."];
Expected Result:
PasswordValidationResult= [false, false, false, false, true]
*/
function validatePasswords(passwords) {
/* let result = [];
for (let i = 0; i < passwords.length; i++) {
if (
containsLowercaseLetter(passwords[i]) &&
containsUppercaseLetter(passwords[i]) &&
containsNumber(passwords[i]) &&
containsSymbol(passwords[i]) &&
passwords[i].length >= 5
) {
if (result.indexOf(passwords[i]) === -1) {
result.push(true);
} else {
result.push(false);
}
} else {
result.push(false);
}
}
return result; */
let result = [];
let seenPasswords = []; // create a new array to keep track of seen passwords
for (let i = 0; i < passwords.length; i++) {
if (
containsLowercaseLetter(passwords[i]) &&
containsUppercaseLetter(passwords[i]) &&
containsNumber(passwords[i]) &&
containsSymbol(passwords[i]) &&
passwords[i].length >= 5
) {
if (seenPasswords.indexOf(passwords[i]) === -1) {
result.push(true);
seenPasswords.push(passwords[i]); // add current password to seen passwords array
} else {
result.push(false);
}
} else {
result.push(false);
}
}
return result;
}
console.log(
validatePasswords(["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty!"])
);
// Returns true if string contains at least one uppercase letter.
function containsUppercaseLetter(string) {
return /[A-Z]/.test(string);
}
//console.log(containsUppercaseLetter("yUhbsdj"));
// Returns true if string contains at least one lowercase letter.
function containsLowercaseLetter(string) {
return /[a-z]/.test(string);
}
// Returns true if string contains at least one number.
function containsNumber(string) {
return /[0-9]/.test(string);
}
// Returns true if string contains at least one symbol.
function containsSymbol(string) {
return /[!#$%.*&]/.test(string);
}
console.log(containsSymbol);
/* ======= TESTS - DO NOT MODIFY ===== */
test("Example 1", () => {
expect(
validatePasswords([
"Se%5",
"TktE.TJTU",
"384#HsHF",
"dvyyeyy!5",
"tryT3729",
])
).toEqual([false, false, true, false, false]);
});
test("Example 2", () => {
expect(
validatePasswords([
"StUFf27%",
"Pl3nty!",
"Jai33",
"shajsaUA**&&",
"Pl3nty!",
])
).toEqual([true, true, false, false, false]);
});