【问题标题】:Random ID function. Length of ID and amount of Id through prompt()随机ID功能。通过 prompt() 得到 ID 的长度和 Id 的数量
【发布时间】: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 的字符串,所以我想知道如何改进这个函数。

感谢帮助,

威利

【问题讨论】:

标签: javascript arrays


【解决方案1】:

看起来你的代码的问题是你在外循环和内循环中使用了相同的变量 i 。这意味着当内部循环完成时,i 的值等于 lengthOfId,这意味着将始终执行 else 块,并且永远不会将 userId 添加到 userIds 数组中。

解决此问题的一种方法是为外循环和内循环使用不同的变量名。例如,您可以将 j 用于内部循环,如下所示:

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 j = 0; j < lengthOfId; j++){
            if( j< lengthOfId  ){
                userId += stringValues.charAt(Math.round(Math.random() * numOfChar))
            }else{
                userIds.push(userId)
            }
        }
    }
    console.log(userIds)
}

您的代码的另一个问题是 else 块仅在 j 等于 lengthOfId 时执行。由于 j 在循环的每次迭代中递增 1,这意味着永远不会执行 else 块。您可以通过将 userIds.push(userId) 语句移到内部循环之外来解决此问题,如下所示:

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 j = 0; j < lengthOfId; j++){
            userId += stringValues.charAt(Math.round(Math.random() * numOfChar))
        }
        userIds.push(userId)
    }
    console.log(userIds)
}

我希望这有帮助!如果您有任何其他问题,请告诉我。

【讨论】:

    【解决方案2】:

    我在 cmets 中更改了一些内容。

    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++) {
    // you need to reset the userId each time you're starting to build a new one, otherwise that's why you'd get a string that has the lenght of x*y
        userId = "";
        for (let i = 0; i < lengthOfId; i++) {
    // there is no need for a if statement on i < lengthOfId, since by definition of your for loop, it's gonna stop before it's the case.
          userId += stringValues.charAt(Math.round(Math.random() * numOfChar));
        }
    // you need to push only once you're done adding all the characters
        userIds.push(userId);
      }
      console.log(userIds);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 2013-01-21
      • 2019-01-28
      • 1970-01-01
      • 2021-04-12
      • 2011-02-20
      • 1970-01-01
      • 2013-04-07
      • 2020-04-29
      相关资源
      最近更新 更多