-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordStrengthChecker.js
More file actions
68 lines (51 loc) · 1.33 KB
/
PasswordStrengthChecker.js
File metadata and controls
68 lines (51 loc) · 1.33 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
/**
************* Problem 3: Password Strength Checker**********
Function name: checkPassword(password)
Rules:
1. Length must be at least 8
2. Must contain at least 1 number
3. Must contain at least 1 uppercase letter
4. Must not contain spaces
Test case 1:
Input: "helloWorld"
Output:
{ valid: false, reasons: ["missing number"] }
*/
function checkPassword(password){
let reason = [];
let hasUpperCase = false;
let hasNumber = false;
let has8Charcter = false;
let hasSpace = password.includes(" ");
let length = password.length;
for(let i = 0; i <length; i++){
if(password[i]>= "A" && password[i]<= "Z"){
hasUpperCase = true;
}
if(password[i]>= "0" && password[i]<="9"){
hasNumber = true;
}
if(length>= 8){
has8Charcter = true;
}
}
if(!hasUpperCase){
reason.push("missing uppercase letter");
}
if(!hasNumber){
reason.push("missing number");
}
if(!has8Charcter){
reason.push("use 8 character or more!")
}
if(hasSpace){
reason.push("Don't use any space!")
}
let isValid = reason.length==0;
return {
valid : isValid,
reason,
};
}
let output = checkPassword("5gt56555P3")
console.log(output);