[英]string random characters in a for loop
Im having trouble with the following code while trying to make a random password generator.我在尝试制作随机密码生成器时遇到以下代码的问题。 The first two lines seem to be working as intended.
前两行似乎按预期工作。 I confirmed this with the following:
我通过以下内容确认了这一点:
If I replace the "passwordText = passwordText.concat(character:" with "console.log(character)" it works in the console. Picks a random char from an array and will console log it the desired amount of times.如果我将“passwordText = passwordText.concat(character:”) 替换为“console.log(character)”,它会在控制台中工作。从数组中选择一个随机字符,并将控制台记录它所需的次数。
However, on line 3 I'm trying to concat these random chars into one string.但是,在第 3 行,我试图将这些随机字符连接成一个字符串。 Any ideas?
有任何想法吗? I'm getting a TypeError: Cannot read property 'concat'.
我收到类型错误:无法读取属性“concat”。 All variables are declared.
声明所有变量。
for (var i = 0; i < inputLength; i++) {
character = finalCriteria[Math.floor(Math.random() * finalCriteria.length)];
passwordText = passwordText.concat(character);
}
passwordText = passwordText.concat(character);
passwordText = passwordText.concat(字符);
I would appreciate any guidance on this.我将不胜感激这方面的任何指导。 Many thanks, Steven.
非常感谢,史蒂文。
PS.附言。 This is my first week with JS, go easy on me: :)
这是我使用 JS 的第一周,go 对我来说很简单::)
strings don't have a concat
method unlike arrays. What you need is +=
:与 arrays 不同,字符串没有
concat
方法。您需要的是+=
:
passwordText += character;
Edit: concat
not push
编辑:
concat
不push
Thanks everyone for your help.谢谢大家的帮助。 Below worked.
下面工作。 I also give password text a value as mentioned by caTS.
我还给密码文本一个 caTS 提到的值。
// function to return the password
function generatePassword() {
for (var i = 0; i < charLength; i++) {
passwordText += finalCriteria[Math.floor(Math.random() * finalCriteria.length)];
} return passwordText
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.