[英]How to reset a randomly generated number again after a click using JavaScript?
So, basically, I am trying to generate a password when I click a button.所以,基本上,当我点击一个按钮时,我试图生成一个密码。 The program generating the password once but when I click it again due to the fact that a new Randomized number isn't generated the program doesn't give a new password.
该程序生成一次密码,但当我再次单击它时,由于未生成新的随机数,该程序不会提供新密码。 Here is the CODE:
这是代码:
let password = ["NyiNE7!38vs","SyiMA4*78dt","SuoSO6]40db","LeaDU9'11ln","QooGU9!09nk","SeuXI1_63sp","SieKY6)07zk","GaaDI9'30gn","BoyLY4|74ct","BuaZI0+69vl"]
function Random() {
let i = Math.floor(Math.random() * 9)
}
i = Math.floor(Math.random() * 9)
const div = document.getElementById('password')
const button = document.getElementById('generate')
button.addEventListener('click' , generatePassword , Random)
function generatePassword(){
div.innerHTML = password[i]
}
Quite a bit going on here, but has some basic errors.这里发生了相当多的事情,但有一些基本错误。
I have fixed those errors so that it creates a password when first run, and when you click the generate button thereafter.我已经修复了这些错误,以便它在第一次运行时创建一个密码,然后当你点击生成按钮时。
let password = ["NyiNE7!38vs","SyiMA4*78dt","SuoSO6]40db","LeaDU9'11ln","QooGU9!09nk","SeuXI1_63sp","SieKY6)07zk","GaaDI9'30gn","BoyLY4|74ct","BuaZI0+69vl"]
// returns a random numbr;
function Random() {
let i = Math.floor(Math.random() * 9)
return i; // note i return the random number!
}
// returns a new password using Random()
function newPw(){
return password[Random()];
}
const div = document.getElementById('password')
const button = document.getElementById('generate')
button.addEventListener('click' , generatePassword)
// if you want a pre-populated password;
generatePassword();
// Sets a new random password
function generatePassword(){
div.innerHTML = newPw();
}
I have shortened this down to use a far more secure method of generating passwords, you can find plenty of examples of random password generators on SO.我已将其缩短为使用更安全的密码生成方法,您可以在 SO 上找到大量随机密码生成器的示例。
const div = document.getElementById('password')
const button = document.getElementById('generate')
button.addEventListener('click' , generatePassword);
// if you want a pre-populated password;
generatePassword();
// taken from https://stackoverflow.com/questions/9719570/generate-random-password-string-with-requirements-in-javascript
function generatePassword(){
var randPassword = Array(10).fill("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").map(function(x) { return x[Math.floor(Math.random() * x.length)] }).join('');var randPassword = Array(10).fill("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").map(function(x) { return x[Math.floor(Math.random() * x.length)] }).join('');
div.innerHTML = randPassword;
}
Your code has some problems, first the variable i is outside the generatePassword method, and hence is not getting updated.您的代码有一些问题,首先变量 i 在 generatePassword 方法之外,因此没有更新。 I have made some changes to your code to make it work, basically call
generatePassword
onclick and inside it call your random function.我对您的代码进行了一些更改以使其工作,基本上调用
generatePassword
onclick 并在其中调用您的随机函数。 You only want to call the functions in an ordered manner when needed.您只想在需要时以有序的方式调用函数。 Find code below:
在下面找到代码:
let password = ["NyiNE7!38vs","SyiMA4*78dt","SuoSO6]40db","LeaDU9'11ln","QooGU9!09nk","SeuXI1_63sp","SieKY6)07zk","GaaDI9'30gn","BoyLY4|74ct","BuaZI0+69vl"]
function getRandom() {
return Math.floor(Math.random() * 9)
}
const div = document.getElementById('password')
const button = document.getElementById('generate')
function generatePassword(){
const randomIndex = getRandom()
div.innerHTML = password[i]
}
button.addEventListener('click' , generatePassword)
Also, as pointed out in the comment, you dont want your password list to be of finite length, rather generate new passwords every time.此外,正如评论中指出的那样,您不希望密码列表的长度有限,而是每次都生成新密码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.