3

How do I create a random string in typescript consisting of the alphabet [a-z0-9]? It should always consist of 32 digits. And also there should be no redundant strings.

1

1 Answer 1

4

Try this:

makeString(): string {
    let outString: string = '';
    let inOptions: string = 'abcdefghijklmnopqrstuvwxyz0123456789';

    for (let i = 0; i < 32; i++) {

      outString += inOptions.charAt(Math.floor(Math.random() * inOptions.length));

    }

    return outString;
  }

  result: string = this.makeString();
Sign up to request clarification or add additional context in comments.

2 Comments

This works for only one time. How do I make it work, so that I can click on a button, that always generates random strings?
Add click event to your button and call the makeString() function. <button (click)="makeString()">Click me</button> I don't know for what you need this function, but now it's uneccessary to return a value... Assign the result to some variable or just console.log it to see if this works for you...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.