JS randomly generated 26 uppercase and lowercase letters _javascript tips

Source: Internet
Author: User
Tags lowercase

First of all to introduce JS to generate 26 uppercase and lowercase letters, mainly used to Str.charcodeat () and String.fromCharCode () method

1. Use charCodeAt () to get the Unicode encoding of a specific character in the string.
2,fromCharCode () can accept one (or more) of the specified Unicode value, and then return the corresponding string.

The Unicode value for the generated uppercase A is the Generatebig_1 function () {var str = [];
 for (Var i=65;i<91;i++) {Str.push (String.fromCharCode (i));
return str;
 The Unicode value for uppercase A is generated as the Generatesmall_1 function () {var str = [];
 for (Var i=97;i<123;i++) {Str.push (String.fromCharCode (i));
return str;
 //Convert string to Unicode code function tounicode (str) {var codes = [];
 for (Var i=0;i<str.length;i++) {Codes.push (Str.charcodeat (i));
return codes;
 function Generatesmall () {var ch_small = ' a ';
 var str_small = ';
 for (Var i=0;i<26;i++) {Str_small + = String.fromCharCode (ch_small.charcodeat (0) +i);
return str_small;
 function Generatebig () {var ch_big = ' A ';
 var str_big = ';
 for (Var i=0;i<26;i++) {Str_big + = String.fromCharCode (ch_big.charcodeat (0) +i);
return str_big;
} console.log (Generatebig ());
 
Console.log (Generatesmall ());
Console.log (Tounicode (Generatebig ()));
 
Console.log (Tounicode (Generatesmall ()));
Console.log (Generatebig_1 ()); Console.log (Generatesmall_1 ()); 

Here to introduce JS randomly generated 26 uppercase and lowercase letters, key line code:

function Getcharacter (flag) { 
 var character= ""; 
 if (flag=== "lower") { 
 character = String.fromCharCode (Math.floor (Math.random () *26) + "a". charCodeAt (0)); 
 if (flag=== "Upper") { 
 character = String.fromCharCode (Math.floor (Math.random () *26) + "A". charCodeAt (0)); 
 return character; 
} 
function Getuppercharacter () {return 
 getcharacter ("Upper");; 
}
function Getlowercharacter () {return 
 getcharacter ("lower");; 
} 
Console.log (Getuppercharacter ());
Console.log (Getlowercharacter ());

The code above enables us to randomly output uppercase letters or small letters, and the principle is very simple, using an interval of uppercase or lowercase Unicode codes.

Code two:

/** 
* Returns a random lowercase letter * 
/ 
function Getlowercharacter () {return 
getcharacter ("lower"); 


/** 
* Returns a random capital letter * 
/ 
function Getuppercharacter () {return 
getcharacter ("Upper"); 


/** 
* Returns a letter 
/function Getcharacter (flag) { 
var character = ""; 
if (flag = = "Lower") { 
character = String.fromCharCode Math.floor (math.random () *) + "a". charCodeAt (0)); 
if (flag = = "Upper") { 
character = String.fromCharCode (Math.floor (Math.random () *) + "A". charCodeAt (0)); c22/>} return 
character 
} 

This article mainly describes how to use JavaScript to achieve output random uppercase or lowercase letters, hoping to bring more or less help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.