JavaScript 8-128位密码生成器concat使用校验及生成按钮无输出问题排查
随机密码生成功能开发问题
预期实现流程
- 密码长度支持范围:8-128位
- 交互逻辑:
- 点击生成按钮后,弹出输入框让用户指定密码长度,校验输入值必须在8-128范围内
- 依次弹出4个确认弹窗,让用户选择密码需要包含的字符类型:大写字母、小写字母、数字、特殊字符
- 根据用户的选择,从对应字符集合中随机抽取字符,生成符合长度要求的密码
当前故障表现
- 所有弹窗可以正常触发弹出
- 点击生成按钮后,页面没有任何密码输出
- 暂不确定
concat方法的使用是否正确,也定位不出是变量定义错误还是整体逻辑问题
原始实现代码
//Lowercase var lowerCase = ['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']; //Uppercase var upperCase = ["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", ]; //Numeric var numValue = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; //Special Characters var spChar = ['@', '%', '+', '\\', '/', "'", '!', '#', '$', '^', '?', ':', ',', ')', '(', '}', '{', ']', '[', '~', '-', '_', '.']; var choices; function generatePassword() { var number = 0 while (number < 8 || number > 128) { number = window.prompt("How long would you like your password to be? Enter a number between 8-128"); console.log(number); } console.log("after ", number) //Prompt for character types var lcChoice = confirm("Would you like your password to contain lowercase characters?"); var ucChoice = confirm("How about uppercase characters?"); var numchoice = confirm("Would you like numbers?"); var spChoice = confirm("How about special characters? Like & or $?"); //If for negative on all 4 options - this doesn't return anything if (!lcChoice && !ucChoice && !numchoice && spChoice) { prompt = alert("You must choose a criteria!"); } //Else if for all 4 character options if (lcChoice && ucChoice && numchoice && spChoice) { choices = spChar.concat(numValue, upperCase, lowerCase); } //Else if for 3 options (lc, uc, n) (uc, n, sp) (n, sp, lc) (sp, lc, uc) else if (lcChoice && ucChoice && numchoice) { choices = lcCase.concat(numValue, upperCase); } else if (lcChoice && ucChoice && spChoice) { choices = lowerCase.concat(spChar, upperCase); } else if (lcChoice && spChoice && numchoice) { choices = lowerCase.concat(numValue, spChar); } else if (spChoice && ucChoice && numchoice) { choices = spChar.concat(numValue, upperCase); } //Else if for 2 options (sp,n) (sp,lc) (sp, uc) (lc, n) (lc, uc) (n, uc) else if (spChoice && numchoice) { choices = spChar.concat(numValue); } else if (spChoice && lcChoice) { choices = spChar.concat(lowerCase); } else if (spChoice && ucChoice) { choices = spChar.concat(upperCase); } else if (lcChoice && numchoice) { choices = lowerCase.concat(numValue); } else if (lcChoice && ucChoice) { choices = lowerCase.concat(upperCase); } else if (numchoice && ucChoice) { choices = numValue.concat(upperCase); } //else if for one choice else if (lcChoice) { choices = lowerCase; } else if (ucChoice) { choices = upperCase; } else if (numchoice) { choices = numValue; } else if (spChoice) { choice = spChar; } var pwd = []; for (i = 0; i === number; i++) { pwd += choices[Math.floor(Math.random() * choices.length)] } return pwd; } //Get references to the #generate element var generateBtn = document.querySelector("#generate"); //Write password to the #password input function writePassword() { var password = generatePassword(); var passwordText = document.querySelector("#password"); passwordText.value = password; } //Add event listener to generate button - This code was given to us generateBtn.addEventListener("click", writePassword);
代码中的问题点
- 核心导致无输出的问题:密码生成循环条件错误
循环初始值i = 0,判断条件为i === number,因为初始i的值和用户输入的长度值不相等(比如用户输入8,此时i=0,0 === 8为false),循环一次都不会执行,直接返回空的pwd数组。正确的判断条件应为i < number。 - 变量名拼写错误
- 三选项(小写、大写、数字)分支中,调用了不存在的
lcCase变量(实际定义的变量名为lowerCase),会直接抛出引用错误,中断后续代码执行。 - 单选项(仅特殊字符)分支中,给未定义的
choice变量赋值(实际要赋值的是choices数组),导致后续取随机字符时choices为undefined,抛出类型错误。
- 三选项(小写、大写、数字)分支中,调用了不存在的
- 全不选字符的判断逻辑错误
原代码判断“所有选项都不选”的条件写成了“仅不选小写、大写、数字,选特殊字符”,完全不符合预期。正确的触发条件应为四个选项全为false时,提示用户至少选择一种字符类型。同时原代码将alert的返回值赋值给prompt全局变量,属于无意义操作。 - 长度校验逻辑不严谨
prompt返回的是字符串类型或null值,没有强制转换为数字类型直接做大小判断,用户输入非数字、点取消时会导致死循环或类型错误。 for循环中的循环变量i没有声明
直接使用未声明的i会生成全局变量,容易引发不可预期的副作用。concat方法本身的使用是正确的
数组的concat方法用于合并多个数组,原代码中除了拼写错误的分支外,其他分支的合并逻辑没有问题。
修复后的可运行代码
// 预定义字符集合 const lowerCase = ['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']; const upperCase = ["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"]; const numValue = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; const spChar = ['@', '%', '+', '\\', '/', "'", '!', '#', '$', '^', '?', ':', ',', ')', '(', '}', '{', ']', '[', '~', '-', '_', '.']; let choices; function generatePassword() { // 校验密码长度 let number = 0; while (true) { const input = window.prompt("请输入密码长度,范围为8-128位"); // 用户点取消则中断流程 if (input === null) return ''; number = parseInt(input); // 校验输入是否为有效数字且在范围内 if (!isNaN(number) && number >=8 && number <=128) break; alert("输入无效,请输入8-128之间的整数"); } // 获取用户选择的字符类型 const lcChoice = confirm("密码是否需要包含小写字母?"); const ucChoice = confirm("密码是否需要包含大写字母?"); const numChoice = confirm("密码是否需要包含数字?"); const spChoice = confirm("密码是否需要包含特殊字符?"); // 校验至少选一种字符 if (!lcChoice && !ucChoice && !numChoice && !spChoice) { alert("请至少选择一种密码包含的字符类型"); return ''; } // 合并字符集合 choices = []; if (lcChoice) choices = choices.concat(lowerCase); if (ucChoice) choices = choices.concat(upperCase); if (numChoice) choices = choices.concat(numValue); if (spChoice) choices = choices.concat(spChar); // 生成密码 let pwd = ''; for (let i = 0; i < number; i++) { pwd += choices[Math.floor(Math.random() * choices.length)]; } return pwd; } // 绑定生成按钮事件 const generateBtn = document.querySelector("#generate"); function writePassword() { const password = generatePassword(); const passwordText = document.querySelector("#password"); passwordText.value = password; } generateBtn.addEventListener("click", writePassword);
内容的提问来源于stack exchange,提问作者michelleH
相关产品推荐
相关产品推荐

