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 ....