简体   繁体   English

单击后如何使用 JavaScript 再次重置随机生成的数字?

[英]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.

相关问题 使用 javascript 随机生成密码后,如何将文本值重置为空字符串 - How Can I reset the Text value to an empty string after I've randomly generated a password with javascript 想要在 JavaScript 中对随机生成的名称列表进行编号 - Wanting to number a list of randomly generated names in JavaScript 如何使用 Javascript 从随机生成的数字数组中省略 0? - How to omit 0 from array of randomly generated numbers using Javascript? 如何使用Javascript防止随机生成的图像重叠 - How to prevent randomly generated images from overlapping using Javascript 如何模拟具有随机生成ID的元素的点击? - How to simulate a click of an element with randomly generated ID? 如何为HTML 5和javascript中的简单猜数字游戏随机设置生成的数字 - How to set a randomly a generated number for a simple guess the number game in HTML 5 and javascript 如果随机生成的数字在 Javascript 中是不受欢迎的数字,我该如何重新生成? - How do I regenerate a randomly generated number if it's an undesirable number in Javascript? 如何点击使用selenium的javascript生成的元素 - How to click on an element generated by javascript using selenium 使用javascript随机生成数字 - Randomly Generating Number using javascript 如何在另一个区域再次显示随机生成的字符串(来自数组)? - How to display a randomly generated string (from array) again in another area?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM