【发布时间】:2022-12-03 15:38:33
【问题描述】:
我尝试构建一个函数,该函数应该输出 x 数量的 id 和 y 长度。 IDs的数量和Ids的长度应由用户通过提示输入。
到目前为止,这是我尝试过的:
function userIdGenerator() {
let amountOfId = prompt('Please enter the amount of IDs')
let lengthOfId = prompt('Please enter the lenght of your ID(s)')
let userId = ''
let userIds = []
let stringValues ='ABCDEFGHIJKLMNOabcdefghijklmnopqrstuvwxyzPQRSTUVWXYZ0123456789'
let numOfChar = stringValues.length
for(let i = 0; i < amountOfId; i++){
for(let i = 0; i < lengthOfId; i++){
if( i< lengthOfId ){
userId += stringValues.charAt(Math.round(Math.random() * numOfChar))
}else{
userIds.push(userId)
}
}
}
console.log(userIds)
}
我得到一个空数组作为输出。当我删除 else 语句和 console.log(userId) 时,我得到一个长度为 x*y 的字符串,所以我想知道如何改进这个函数。
感谢帮助,
威利
【问题讨论】:
-
if(i < lengthOfId)永远是真的。 -
比
prompt更喜欢<input>。
标签: javascript arrays