I'm making a course and one of the tasks is to generate four different passwords that should fill the four different paragraphs <p id="first"></p>
, <p id="second"></p>
, <p id="third"></p>
, <p id="fourth"></p>
I have created a script that creates a password but somehow it doesn't print the first password. When I do console.log(password)
it shows that creates a password on the console. But when I try to put first.textContent = password
it shows nothing.
let chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "A", "B", "C", "D"]
let number = 10
let password = ""
let first = document.getElementById("first")
let second = document.getElementById("second")
let third = document.getElementById("third")
let fourth = document.getElementById("fourth")
function generate() {
for (let i = 0; i < number; i++) {
let passwordChar = Math.floor(Math.random() * chars.length)
password += chars[passwordChar]
return password
}
}
first.textContent = password
<html>
<head>
<link rel="stylesheet" href="index.css">
</head>
<body>
<h1>Generate a random password</h1>
<p id="description">Never use an insecure password again</p>
<button onclick="generate()">Generate passwords</button>
<p id="line"></p>
<p id="first"></p>
<p id="second"></p>
<p id="third"></p>
<p id="fourth"></p>
</body>
</html>
I'm also stuck to generate each time the function is running a new password and put it on the next paragraph. Like so:
first.textContent = password
second.textContent = password
third.textContent = password
fourth.textContent = password
Any help will be appreciated