-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6422 from nox456/main
Reto #3 - Javascript
- Loading branch information
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
Retos/Reto #3 - EL GENERADOR DE CONTRASEÑAS [Media]/javascript/nox456.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
const parameters = ["uppercase", "numbers", "symbols"]; | ||
|
||
const parametersCount = Math.trunc(Math.random() * 3) + 1; | ||
|
||
const letters = { | ||
1: "a", | ||
2: "b", | ||
3: "c", | ||
4: "d", | ||
5: "e", | ||
6: "f", | ||
7: "g", | ||
8: "h", | ||
9: "i", | ||
10: "j", | ||
11: "k", | ||
12: "l", | ||
13: "m", | ||
14: "n", | ||
15: "o", | ||
16: "p", | ||
17: "q", | ||
18: "r", | ||
19: "s", | ||
20: "t", | ||
21: "u", | ||
22: "v", | ||
23: "w", | ||
24: "x", | ||
25: "y", | ||
26: "z", | ||
}; | ||
|
||
const symbols = { | ||
1: ".", | ||
2: ",", | ||
3: ";", | ||
4: ":", | ||
5: "-", | ||
6: "_", | ||
7: "[", | ||
8: "]", | ||
9: "{", | ||
10: "}", | ||
11: "*", | ||
12: "?", | ||
13: "¿", | ||
14: "¡", | ||
15: "!", | ||
16: "$", | ||
17: "/", | ||
18: "(", | ||
19: ")", | ||
20: "=", | ||
21: "&", | ||
22: "#", | ||
}; | ||
|
||
const genPassword = (parametersCount) => { | ||
let hasUppercase = false; | ||
let hasNumbers = false; | ||
let hasSymbols = false; | ||
|
||
for (let i = 0; i < parametersCount; i++) { | ||
const parameter = parameters[Math.trunc(Math.random() * 3)]; | ||
if (parameter == "uppercase") { | ||
hasUppercase = true; | ||
} else if (parameter == "numbers") { | ||
hasNumbers = true; | ||
} else { | ||
hasSymbols = true; | ||
} | ||
} | ||
const passLength = Math.trunc(Math.random() * 9) + 8; | ||
let password = ""; | ||
for (let i = 0; i < passLength; i++) { | ||
const parameterSelected = Math.trunc(Math.random() * 3) + 1; | ||
if (parameterSelected == 1) { | ||
const letter = | ||
letters[ | ||
Math.trunc(Math.random() * Object.entries(letters).length) + | ||
1 | ||
]; | ||
if (hasUppercase) { | ||
password = password.concat( | ||
Math.trunc(Math.random() * 2) == 0 | ||
? letter | ||
: letter.toUpperCase() | ||
); | ||
} else { | ||
password = password.concat(letter); | ||
} | ||
} else if (parameterSelected == 2 && hasNumbers) { | ||
password = password.concat(Math.trunc(Math.random() * 10)); | ||
} else if (hasSymbols) { | ||
const symbol = | ||
symbols[ | ||
Math.trunc(Math.random() * Object.entries(symbols).length) + | ||
1 | ||
]; | ||
password = password.concat(symbol); | ||
} else { | ||
const letter = | ||
letters[ | ||
Math.trunc(Math.random() * Object.entries(letters).length) + | ||
1 | ||
]; | ||
if (hasUppercase) { | ||
password = password.concat( | ||
Math.trunc(Math.random() * 2) == 0 | ||
? letter | ||
: letter.toUpperCase() | ||
); | ||
} else { | ||
password = password.concat(letter); | ||
} | ||
} | ||
} | ||
return password | ||
}; | ||
|
||
console.log(genPassword(parametersCount)); |