Two small functions ....

Source: Internet
Author: User

Today I learned two functions ...

--------------------------------------------------------------------------------------------------------------- --------------

  First, sort a number array:

var arr = [10,75,44,32,30,7]; Arbitrarily declare an array with all the numbers inside it (this is just one way of declaring the array);

Define a compare function and pass in a A, b two parameter;

function Compare (A, b) {

if (a > B) {//Determine the size of the parameter, A and a, if A>b is true, that is, the expression is established;

return 1; then return 1; (1 for Boolean value true);

}else if (a < b) {//if A<B is established;

return-1; then return-1; (-1 for Boolean value false);

}else{//Other cases

return 0; Then it returns 0;

}

}

The Compare function above only compares the size of a, b two number, and does not give them the sort of behavior;

The sort behavior is done by the sort function;

document.write (Arr.sort (Compare)); The sequential array of arr is re-exported to the page;

--------------------------------------------------------------------------------------------------------------- --------------

  Capitalize the English letter in a string into lowercase and lowercase:

var str = "Hello world!"; Write a string of uppercase and lowercase letters;

function Change () {

for (var i = 0;i < str.length;i++) {//define a variable i in a For loop, and assign the initial value i = 0;i less than the character length of the string str, after each execution of the loop i + 1;

var c = Str.charat (i); Define a variable C, assigned to each of the possible characters in the string; charAt (i) function is the character that returns the index of I in the string;

if (c >= ' a ' && C <= ' Z ') {//If C is one of the uppercase letters A-Z;

document.write (C.tolowercase ()); Then the uppercase string is changed to lowercase and output; the toLowerCase () function is a function that capitalizes uppercase;

}else if (c >= ' a ' && c <= ' z ') {//If C is one of the lowercase letters A-Z;

document.write (C.touppercase ()); Then the lowercase string is changed to uppercase and output; the toUpperCase () function is a function that turns lowercase into uppercase;

}

}

}

Change (); Execute this function;

--------------------------------------------------------------------------------------------------------------- ---------------

The above for today's harvest ....

Two small functions ....

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.