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 Answer
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();
2 Comments
Michael
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?
Srdjan
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...