2 回答
![?](http://img1.sycdn.imooc.com/545869390001b87802200220-100-100.jpg)
TA贡献2041条经验 获得超4个赞
您只需要在循环外声明密码变量
len 是数字,所以不需要 len.length,只需输入 len
char[i] - 只使用 char,所以我们可以把它变成一行,没有实际的 char 变量 password+=String.fromCharCode(random)
我建议提供 len 作为参数,所以只需调用
console.log( createPassword(16) )
这里函数本身
function createPassword(len){
var password = '';//declare
for(let i=0; i<len; i++){
var random = Math.floor(Math.random()*94)+33
password += String.fromCharCode(random)
}
return password;
}
![?](http://img1.sycdn.imooc.com/545864490001b5bd02200220-100-100.jpg)
TA贡献1817条经验 获得超6个赞
document.getElementById('generate').addEventListener('click', createPassword)
function createPassword(){
var len= document.getElementById('length').value;
var password=''
for(let i=0; i<len; i++){
var random= Math.floor(Math.random()*94)+33
password+= String.fromCharCode(random)
}
document.getElementById('result').innerText=password;
}
这是我正在尝试做的工作版本,以供将来参考。
它生成一个带有输入长度的随机字符/数字的密码
添加回答
举报