Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 954ee2d

Browse files
70 solved
1 parent d38026b commit 954ee2d

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

‎problem70/problme70.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
1-
/* Create a program that generates a random password of a specified length. The password should include a mix of uppercase letters, lowercase letters, numbers, and special characters. */
1+
/* Create a program that generates a random password of a specified length. The password should include a mix of uppercase letters, lowercase letters, numbers, and special characters. */
2+
3+
const randomPasswordGenerator = length => {
4+
const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
5+
const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz';
6+
const numberChars = '0123456789';
7+
const specialChars = '!@#$%^&*()_-+=<>?/{}~';
8+
9+
const allChars = uppercaseChars + numberChars + lowercaseChars + specialChars;
10+
11+
let password = '';
12+
13+
if(length < 4){
14+
return 'Error: password length should be 4 characters or above.';
15+
16+
}
17+
18+
while (true) {
19+
for (let i = 0; i < length; i++) {
20+
const randomIndex = Math.ceil(Math.random() * allChars.length);
21+
password += allChars.charAt(randomIndex);
22+
}
23+
24+
if (/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_\-+=<>?/{}~])/.test(password)) {
25+
break;
26+
}
27+
28+
password = '';
29+
}
30+
31+
return password;
32+
}
33+
34+
const passwordLength = 10;
35+
const randomPassword = randomPasswordGenerator(passwordLength);
36+
console.log(randomPassword);
37+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /