This repository was archived by the owner on Apr 18, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathpassword-validator.test.js
More file actions
98 lines (79 loc) · 2.18 KB
/
password-validator.test.js
File metadata and controls
98 lines (79 loc) · 2.18 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
/*
Password Validation
Write a program that should check if a password is valid
and returns a boolean
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.
You must breakdown this problem in order to solve it. Find one test case first and get that working
*/
function isPasswordValid(str) {
let points = 6;
if (str.length < 5) {
points--;
}
if (str.toUpperCase() === str) {
points--;
}
if (str.toLowerCase() === str) {
points--;
}
for (let char of str) {
if (isNaN(Number(char))) {
points--;
}
}
if (
!str.includes("!") &&
!str.includes("#") &&
!str.includes("$") &&
!str.includes("%") &&
!str.includes(".") &&
!str.includes("*") &&
!str.includes("&")
) {
points--;
}
if (points != 5) {
return false;
} else {
return true;
}
}
test("Have at least 5 characters", function () {
const input = "Aa1.";
const currentOutput = isPasswordValid(input);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
test("Have at least one English uppercase letter (A-Z)", function () {
const input = "aaa1.";
const currentOutput = isPasswordValid(input);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
test("Have at least one English lowercase letter (a-z)", function () {
const input = "AAA1.";
const currentOutput = isPasswordValid(input);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
test("Have at least one number (0-9)", function () {
const input = "Aaaa.";
const currentOutput = isPasswordValid(input);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
test(
"Have at least one non-alphanumeric symbol (!, #, $, %, ., *, " & ")",
function () {
const input = "Aaaa1";
const currentOutput = isPasswordValid(input);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
}
);