This repository was archived by the owner on Oct 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy path7-password-validator.js
More file actions
115 lines (93 loc) · 3.36 KB
/
7-password-validator.js
File metadata and controls
115 lines (93 loc) · 3.36 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
/*
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 ("!", "#", "$", "%", ".", "*", "&")
Passwords must not be any previous password in the passwords array.
Example 1:
PreviousPassword = ["fhD8!yrjj", "ttkTu.wer3", "dvyyeyY!5", "qwbfj76%", "tytT3729."];
Expected Result:
PasswordValidationResult= [false, false, false, false, true]
*/
const IS_UPPER = 1
const IS_LOWER = 2
const IS_NUMBER = 4
const IS_NON_APLHANUM = 8
const IS_INVALID_CHAR = 16
const VALID_PASSWD = 15
/*
* validChars(char) - takes a character as input and tests whether it is lowercase, uppercase,
* a digit (0 - 9), or a strict set of alphanumerics ("!", "#", "$", "%", ".").
* All tests performed use regular expressions
*/
function isValidChar(char) {
if (/^[a-z]/.test(char))
return IS_LOWER
if (/^[A-Z]/.test(char))
return IS_UPPER
// if (Number.isInteger(Number.parseInt(char)))
if (/^[0-9]/.test(char))
return IS_NUMBER
// if (["!", "#", "$", "%", "."].includes(char))
if (/^[!#$%.]/.test(char))
return IS_NON_APLHANUM
return IS_INVALID_CHAR
}
/*
* isValidPassword(password, index, passwdArray) -
* password - current password from array of passwords
* index - password's current array index
* passwdArray - the array of passwords
*
*
*/
function isValidPassword(password, index, passwdArray) {
let flags = 0
if (password.length >= 5) { // All valid passwords are at least 5 characters long
if (passwdArray.indexOf(password) === index) { // filter out duplicates
/*
use reduce() to store the result of accumulated bit-wise OR operations on the flags variable
validChars(char) can return any of the following:
IS_LOWER
IS_UPPER
IS_NUMBER
IS_NON_APLHANUM
IS_INVALID_CHAR
*/
return password.split("").reduce((flags, char) => flags | isValidChar(char), 0) === VALID_PASSWD
}
}
return false
}
function validatePasswords(passwords) {
return passwords.map(isValidPassword)
}
/* ======= TESTS - DO NOT MODIFY ===== */
const passwords1 = ["Se%5", "TktE.TJTU", "384#HsHF", "dvyyeyy!5", "tryT3729"]
const passwords2 = ["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty!"]
const util = require('util');
function test(test_name, actual, expected) {
let status;
if (util.isDeepStrictEqual(actual, expected)) {
status = "PASSED";
} else {
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
}
console.log(`${test_name}: ${status}`);
}
test(
"validatePasswords function works - case 1",
validatePasswords(passwords1),
[false, false, true, false, false]
);
test(
"validatePasswords function works - case 2",
validatePasswords(passwords2),
[true, true, false, false, false]
);