I am doing exercises in javascript and i came across a problem. the user should choose the character length in the password however if i select 4 characters in the input I get 3. why?
my thought was to be useful to choose the number of characters and to generate a password as many characters as there are. where I am wrong I ask for help!
function generirajLozinku(pLength) {
var keyListAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
keyListInt = "123456789",
keyListSpec = "",
password = '@';
var len = Math.ceil(pLength / 2); // mijenanjem ovog broja mijenja se duzina lozinke
len = len - 1;
var lenSpec = pLength - 2 * len;
for (i = 0; i < len; i++) {
password += keyListAlpha.charAt(Math.floor(Math.random() * keyListAlpha.length));
password += keyListInt.charAt(Math.floor(Math.random() * keyListInt.length));
}
for (i = 0; i < lenSpec; i++)
password += keyListSpec.charAt(Math.floor(Math.random() * keyListSpec.length));
password = password.split('').sort(function() {
return 0.5 - Math.random()
}).join('');
return password;
}
function myFunction() {
var x = document.getElementById("num").value;
document.getElementById("demo2").innerText = generirajLozinku(x);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Document</title>
</head>
<body>
<section>
<div>
<h1>Your strong password marker</h1>
<p>Password length</p>
<input type="number" name="izaberi boj karaktera!" id="num" min="4" max="10">
<br>
<button onclick="myFunction()">Generate</button>
<p id="demo2">Your password</p>
</div>
<div>
</div>
</section>
</body>
</html>