用于确定生成密码长度的用户输入

问题描述 投票:0回答:1

我不太清楚如何制作,因此生成的密码长度与用户说的应该的一样长。密码生成正常,我只需要帮助实现用户输入]

function start(){
    passwordLength();
    passwordGenerator();
}

function passwordLength(){
    var length = readInt("How many characters long would you like your password to be?: ");
}

function passwordGenerator(length) {
   var result = '';
   var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
   var charactersLength = characters.length;
   for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
   }
   return result;
}

console.log(passwordGenerator(6));
javascript generator user-input
1个回答
0
投票

这样的东西

function start() {
  var length = passwordLength();
  console.log(length)
  console.log(passwordGenerator(length))
}

function passwordLength() {
  return prompt("How many characters long would you like your password to be?: ");
}

function passwordGenerator(length) {
  var result = '';
  var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  var charactersLength = characters.length;
  for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
}

start();

在调用启动函数的地方,使用prompt获取长度,然后将值发送到passwordGenerator函数。

© www.soinside.com 2019 - 2024. All rights reserved.