Js randomly generates 26 uppercase/lowercase letters and js26 lowercase letters

Source: Internet
Author: User

Js randomly generates 26 uppercase/lowercase letters and js26 lowercase letters

First, we will introduce JavaScript to generate 26 uppercase and lowercase letters, mainly using the str. charCodeAt () and String. fromCharCode () methods.

1. UseCharCodeAt ()To obtain the Unicode encoding of a specific character in a string.
2,FromCharCode ()You can accept one or more specified Unicode values, and then return the corresponding string.

// Generate the Unicode value of uppercase letter A as 65 function generateBig_1 () {var str = []; for (var I = 65; I <91; I ++) {str. push (String. fromCharCode (I);} return str;} // generate the Unicode value of uppercase letter a as 97 function generateSmall_1 () {var str = []; for (var I = 97; I <123; I ++) {str. push (String. fromCharCode (I);} return str;} // convert the string to the 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 ());

The following describes how to randomly generate 26 uppercase/lowercase letters and key lines of code for javascript:

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 above Code meets our requirements and can randomly output uppercase or lowercase letters. The principle is very simple, that is, the use of the range of uppercase or lowercase letters Unicode code.

Code 2:

/*** Return a random lowercase letter */function getLowerCharacter () {return getCharacter ("lower ");;} /*** return a random uppercase letter */function getUpperCharacter () {return getCharacter ("upper ");;} /*** returns a letter */function getCharacter (flag) {var character = ""; if (flag = "lower") {character = String. fromCharCode (Math. floor (Math. random () * 26) + "". charCodeAt (0);} if (flag = "upper") {character = String. fromCharCode (Math. floor (Math. random () * 26) + "". charCodeAt (0);} return character ;}

This article describes how to use javascript to output random uppercase or lowercase letters, hoping to help you more or less.

Articles you may be interested in:
  • ASP generates random string (numbers + uppercase/lowercase letters) Code
  • Asp.net easy to generate registration code (numbers + uppercase/lowercase letters)
  • Java generates a Random verification code (supporting uppercase/lowercase letters, numbers, and random fonts ).
  • How to generate random uppercase/lowercase letters using javascript
  • Javascript generates uppercase and lowercase letters

Related Article

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.