How to generate random lowercase letters in TypeScript

2 Answers

0 votes
function GetRandomLowercaseString(n: number) {
    let s: string = "";
    const letters: string = "abcdefghijklmnopqrstuvwxyz";
  
    for(let i: number = 0; i < n; i++ ) {
        s += letters.charAt(Math.floor(Math.random() * letters.length));
    }
  
    return s;
}
 
console.log(GetRandomLowercaseString(10));
 
 
 
/*
run:
 
"ginfpnkijl"
 
*/

 



answered Oct 10, 2024 by avibootz
0 votes
function getRandomLowercaseLetter() {
    const randomCharCode = Math.floor(Math.random() * 26) + 97; // 97 is ASCII/Unicode code for 'a'
     
    return String.fromCharCode(randomCharCode);
}
 
function GetRandomLowercaseString(n: number) {
    let s: string = "";
 
    for(let i: number = 0; i < n; i++ ) {
        s += getRandomLowercaseLetter()
    }
  
    return s;
}
 
console.log(GetRandomLowercaseString(10));
 
 
 
/*
run:
 
"iuryercnnx" 
 
*/

 



answered Oct 10, 2024 by avibootz

Related questions

1 answer 104 views
1 answer 97 views
1 answer 94 views
1 answer 97 views
3 answers 148 views
2 answers 133 views
3 answers 329 views
...