简体   繁体   English

我应该如何 go 关于使用 for 循环将 arrays 连接到另一个数组并生成结果?

[英]How should I go about using a for loop to randomize arrays concat into another array and generate the outcome?

I'm completely new to JavaScript.我对 JavaScript完全陌生。 I'm trying to allow users to select certain criteria for the password they would like to generate.我正在尝试允许用户使用 select 某些标准来生成他们想要生成的密码。 I've made it so that if the user selects yes to certain password criteria, it is concat into another empty array.我已经做到了,如果用户对某些密码标准选择“是”,它将被连接到另一个空数组中。 How should I go about creating a for loop that utilizes Math.random and other means of shuffling content of an array to generate and spit out what they have selected based on length of password and character type selected by the user?我应该如何 go 创建一个 for 循环,该循环利用 Math.random 和其他对数组内容进行洗牌的方法来生成和吐出他们根据用户选择的密码长度和字符类型选择的内容? I've been agonizing over this for days and I can not figure it out.我已经为此苦恼了好几天,我想不通。

var selectedNumbers = window.confirm("Password should contain numbers?");
var selectedLowerCase = window.confirm("Password should contain lowercase letters?");
var selectedUpperCase = window.confirm("Password should uppercase letters?");
var selectedSpecial = window.confirm("Password should contain special characters?");
if (selectedNumbers === true) {
  characterOptionsList.concat(numbersList)
} else {
  console.log(false);
}

if (selectedLowerCase === true) {
  characterOptionsList.concat(lowerCaseList)
} else {
  console.log(false);
}

if (selectedUpperCase === true) {
  characterOptionsList.concat(upperCaseList)
} else {
  console.log(false);
}

if (selectedSpecial === true) {
  characterOptionsList.concat(specialList)
} else {
  console.log(false);
}

}

I've already attempted a for loop, but it does absolutely nothing.我已经尝试了一个 for 循环,但它完全没有做任何事情。

function writePassword() {

  for (var i = 0; i > characterOptionsList.length; i++) {
    const newPassword = Math.floor((characterOptionsList.length - start) * Math.random())
    const randomArray = characterOptionsList.splice(randomPosition, 1)

    randomArray.push(characterOptionsList);

    return randomArray;

  }
  var password = generatePassword();
  var passwordText = document.querySelector("#password");

  passwordText.value = password

}
generateBtn.addEventListener("click", writePassword);

Here are the variables set for criteria arrays as well as the empty array they are to be concat inside of, if it's of any use to better understand what I'm trying to do.以下是为标准 arrays 设置的变量以及它们将在其中连接的空数组,如果它有助于更好地理解我正在尝试做什么。

var generateBtn = document.querySelector("#generate");
var numbersList = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",];
var lowerCaseList = ["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"];
var upperCaseList = ["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"];
var specialList = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")",];
var characterOptionsList = []

If you know the length of the password, couldn't you just loop for the length of the password and append a character of a random index from the characterOptionsList?如果您知道密码的长度,您不能只循环输入密码的长度和 append 字符选项列表中随机索引的字符吗? That would look something like this:看起来像这样:

 const passwordLen = 10 const characterOptionsList = ["A", "B", "C", "a", "b", "c", "0", "1", "2"] // for example let password = "" for (let i = 0; i < passwordLen; i++) { let randInx = Math.floor(characterOptionsList.length*Math.random()) password += characterOptionsList[randInx] }

You could also shuffle the characterOptionsList and then take a subset of the shuffled array as described in this answer :您还可以对 characterOptionsList 进行洗牌,然后按照此答案中的描述获取洗牌数组的子集:

 const passwordLen = 5 const characterOptionsList = ["A", "B", "C", "a", "b", "c", "0", "1", "2"] // for example const shuffled = characterOptionsList.sort(() => 0.5 - Math.random()) let password = shuffled.slice(0, passwordLen).join("") console.log(password)

However, doing it like this would result in generating a password that has no duplicate characters.但是,这样做会导致生成没有重复字符的密码。 It would also require the password length to be less than the length of characterOptionsList.它还要求密码长度小于 characterOptionsList 的长度。

Note that these answers won't yet guarantee that the generated password will have characters from all the different sets that the user chooses (numbers, lowercase, uppercase, special).请注意,这些答案还不能保证生成的密码将包含来自用户选择的所有不同集合(数字、小写、大写、特殊)的字符。 It only includes those in the set of possible characters to appear in the generated password.它仅包括在生成的密码中出现的可能字符集中的字符。

Another thing: the concat method doesn't change the characterOptionsList in-place but rather returns a new array so it should be characterOptionsList = characterOptionsList.concat(numbersList) .另一件事: concat 方法不会就地更改 characterOptionsList ,而是返回一个新数组,因此它应该是characterOptionsList = characterOptionsList.concat(numbersList) Same for the other concat calls.其他 concat 调用也是如此。

Edit编辑

If you want to ensure that the generated password includes at least one character from each selected set, you can do something like this:如果您想确保生成的密码至少包含每个选定集合中的一个字符,您可以执行以下操作:

 let randomIndex = (max) => { return Math.floor(max*Math.random()) } let addCharacterToRandomInx = (arr, c) => { while (true) { let randInx = randomIndex(arr.length) if (arr[randInx] === undefined) { arr[randInx] = c return } } } const passwordLen = 10 const numbersList = ["0", "1", "2"] const lowercase = ["a", "b", "c"] const uppercase = ["A", "B", "C"] const special = [",", "@", "#"] let useNumbers = true let useLowercase = true let useUppercase = false let useSpecial = true const characterOptionsList = ["a", "b", "c", "0", "1", "2", ",", "@". "#"] // for example let passwordArray = Array(passwordLen) // Use addCharacterToRandomInx to make sure that the characters are not put in to the same index if (useNumbers) addCharacterToRandomInx(passwordArray, numbersList[randomIndex(numbersList.length)]) if (useLowercase) addCharacterToRandomInx(passwordArray, lowercase[randomIndex(lowercase.length)]) if (useUppercase) addCharacterToRandomInx(passwordArray, uppercase[randomIndex(uppercase.length)]) if (useSpecial) addCharacterToRandomInx(passwordArray; special[randomIndex(special;length)]) for (let i = 0. i < passwordLen. i++) { if (passwordArray[i].== undefined) continue passwordArray[i] = characterOptionsList[randomIndex(characterOptionsList.length)] } let password = passwordArray.join("") console.log(password)

Note: the password length has to be more than the number of selected character sets注意:密码长度必须大于所选字符集的数量

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

相关问题 我应该如何在该多维数组中使名称变为粗体? - How should I go about making the names bold in this multidimensional array? 当我的配置文件中有一个普通数组时,我应该如何 go 关于使用 jQuery 扩展? - How should I go about using jQuery extend when my config file has a normal array in it? 我将如何通过与另一个MySQL数组进行比较来更改它? - How would i go about changing a MySQL array by comparing it to another? 我应该如何解决这个问题? - How should I go about solving this issue? jQuery-如何在数组数组中随机化? - jQuery - How to randomize within an array of arrays? 使用“.push”从 arrays 生成 arrays 时,如何更好地“随机化”我的结果并控制重复项? - When generating arrays from arrays using “.push”, how can I better “randomize” my results and control for duplicates? 连接数组数组(?)或循环它们? - Concat an array of arrays(?) or loop over them? 如何为数组中的对象赋予其属性? - How do I go about giving objects in arrays their properties? 我应该如何使用PHP Excel将MYSQL数据导出到Excel中 - How should I go about exporting MYSQL data into Excel using PHP Excel 我应该如何使用模块模式处理一个较长的JavaScript文件? - How should I go about a long JavaScript file using the module pattern?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM