3

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

  • 1
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Dec 03 '22 at 07:28
  • `if(i < lengthOfId)` will always be true. – Sebastian Simon Dec 03 '22 at 07:30
  • Prefer an `` over `prompt`. – jsejcksn Dec 03 '22 at 07:30
  • I wonder how important the generated IDs are? I mean, generating random strings is one thing, but in most cases IDs should be unique. Meaning that your generator should make it as unlikely as possible to ever generate the same ID twice. If that's the case, then this is definitely the wrong approach. – icecub Dec 03 '22 at 08:03
  • @icecub it is just for a coding task. Not for a real application. but in another task i had to create unique id's :) – willy chrosnik Dec 03 '22 at 08:46
  • Ah. Well in that case the main issue is that you use the variable `i` in both loops. Since `i` is already declared in the first loop, your second loop will overwrite this. Hence you should use a different variable name there, like `j` or `y`. Anything is fine as long as you're not using it anywhere else. Other than that you can remove the `else` conditional from your second loop and push `userId` outside of it. – icecub Dec 03 '22 at 08:54

1 Answers1

1

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);
}
Emilien
  • 614
  • 1
  • 5
  • 17