Home > Back-end >  Random ID function. Length of ID and amount of Id through prompt()
Random ID function. Length of ID and amount of Id through prompt()

Time:12-03

i try to build a function that should output x-amounts of ids with a y-lenght. The amount of Ids and length of Ids shall be input from the user through prompt.

Here is what i have tried so far:

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)
}

I get an empty array as output. When i delete the else-statement and console.log(userId) i get a string that has the lenght of x*y so i wander how i can improve this function.

Thanks for help,

Willy

CodePudding user response:

It looks like the issue with your code is that you're using the same variable i for the outer and inner loop. This means that when the inner loop finishes, the value of i is equal to lengthOfId, which means that the else block will always be executed and the userId will never be added to the userIds array.

One way to fix this issue would be to use different variable names for the outer and inner loops. For example, you could use j for the inner loop, like this:

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)
}

Another issue with your code is that the else block is only executed when j is equal to lengthOfId. Since j is incremented by 1 in each iteration of the loop, this means that the else block will never be executed. You can fix this issue by moving the userIds.push(userId) statement outside of the inner loop, like this:

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)
}

I hope this helps! Let me know if you have any other questions.

CodePudding user response:

A few things I changed in the comments.

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);
}
  • Related