简体   繁体   English

JavaScript密码生成器-数组中的多个随机值

[英]JavaScript Password Generator - multiple random values from array

I am a newcomer to JavaScript, and have been battling with this relatively simple program for a few days now. 我是JavaScript的新手,并且与这种相对简单的程序进行了几天的战斗。

The program: I have a website with a button on. 该程序:我有一个带有按钮的网站。 When you click the button, a password is generated (from an array) and displayed for the user. 当您单击按钮时,将生成密码(从阵列)并为用户显示密码。

The problem: "function generate()" displays the single same generated character 10 times, instead of 10 different characters 10 times, which naturally would make a much stronger password. 问题是:“ function generate()”将单个相同的生成字符显示10次,而不是10个不同的字符显示10次,这自然会使密码更强。 I want it to randomize 10 different characters into a password, not the same character 10 times. 我希望它将10个不同的字符随机化成一个密码,而不是10个相同的字符。 So the program works, but not as I intended. 因此该程序可以正常运行,但不符合我的预期。

I have created a JSfiddle containing comments for display. 我创建了一个包含要显示的注释的JSfiddle。 Note that document.write is disabled in JSfiddle, so the result is set to show in console. 请注意,在JSfiddle中禁用document.write,因此结果设置为在控制台中显示。

https://jsfiddle.net/ohstmbym/ https://jsfiddle.net/ohstmbym/

<!DOCTYPE html>
<html>
  <head>
  <title></title>
  <button onclick="generate()">Button</button>
  </head>
<body>
<script>
  var arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
  randomLetter = arr[Math.floor(arr.length * Math.random())];
  function generate () {for (i = 0; i < 10; i++) {document.write(randomLetter); } }
</script>
</body>
</html>

Important : I would like to understand what is wrong with the code/how to fix it - I am not just looking for somebody to solve it for me with no explanation. 重要提示 :我想了解代码的问题/如何解决-我不仅在寻找可以解决我问题的人,而且没有任何解释。 If you want to help me solve the problem, I am very thankful, but please explain what you do. 如果您想帮助我解决问题,我非常感谢,但是请解释您的工作。 And also, please don't rewrite the code completely like in rewriting the entire program to make it work; 而且,请不要像重写整个程序那样完全重写代码,以使其正常工作。 I am looking for solutions/explanations, not somebody write the whole thing up. 我在寻找解决方案/解释,而不是有人把整个事情写下来。 If it's completely wrong and need to be entirely rewritten, please simply explain in words what should've been done instead. 如果完全错误并且需要完全重写,请简单地用言语解释应该做些什么。

Still, thank you very much in advance, I appreciate any help. 不过,非常感谢您,我非常感谢。

(PS I know only lower case letters for password generation will be of weaker security, but this is only for experimentation) (PS我知道,只有小写字母才能生成密码,其安全性较弱,但这仅用于实验)

The problem is that you're not regenerating the random element , you're caching a random element once in randomLetter so it's always the same. 问题是您没有重新生成random元素 ,而是在randomLetter缓存了一次 random元素,因此它总是相同的。 The value of the variable will not change every time you refer to it. 变量的值不会在每次引用时更改 Put it inside the for loop to regenerate the number every iteration: 将其放入for循环中可在每次迭代时重新生成数字:

var arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
function generate () {
  for (i = 0; i < 10; i++) {
    var randomLetter = arr[Math.floor(arr.length * Math.random())];
    document.write(randomLetter); 
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM