<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script>// 1、封装函数 检查字符串结尾: 例如:输入confirmEnding("He has to give me a new name", "me")返回truefunction confirmEnding(str, strPart) { // 获取长度var strLen = str.length, strPartLen = strPart.length; NUM = strLen -vardefinition string starts from what position taken// strPartLen; // . 3 methods. Slice (), the substring (), substr () // IF (strpart == str.slice (NUM)) { // return to true; // } the else { // return to false; // } return (strpart === str.slice (NUM)); } the console.log (confirmEnding ( "Me of He has to give new new name A", "ffme" )); // 2, the digital wrapper function inverts example: input reverseNumber (123456) ; Back 654321 function reverseNumber (NUM) { // ideas: method string is not inverted, the inverted first converted to an array, then the array into a string after reversing Split () reverse () the Join () var + NUM = NUM ""; //Number into the string var arrNum num.split = ( "") Reverse ();. // string into the array and inverting var strNum = + (arrNum.join ( "")); // the number of assembly transducer and converted into a string Number The return strNum; } the console.log (reverseNumber ( 123456 )); // . 3, determines whether the both sides symmetrically. Input isPalindrome ( 'hello'); return false; input isPalindrome ( 'madam'); return to true function isPalindrome (STR) { // odd situation: 357,911 @ 1.5 2.5 3.5 4.5 5.5 rounded up // even number where: len / 2 minutes on both sides, while the reverse, whether or not the same on both sides var len = str.length; var NUM, str1; ] Function substrings (str) { were arr = []; was len = str.length for ( var I = 0; I <len; I ++ ) { for ( var J =. 1; J <= len - I; J ++ ) { arr.push (str.substr (I, J)) } } return ARR } the console.log (substrings ( 'Dog' )); // . 5, a write function on the incoming string re e.g. alphabetical input reorderStr ( 'webmaster'); return abeemrstw function reorderStr (STR) { return STR .split ( ""). Sort () the Join. ( ""); // into an array, sorted, and then converted to a string } the console.log (reorderStr ('webmaster' )); // . 6, a function of writing the first letter of the incoming string of each word input myCap ( 'the quick brown fox' ); Quick Brown Fox return of The function myCap (STR) { var = str.split arrStr ( ""); // via "" into an array var len = arrStr.length; // iterate take the first letter of each one for ( var I = 0; I <len; I ++ ) { arrStr [I] = arrStr [I] .substring (0,. 1) .toUpperCase () + arrStr [I] .substring (. 1 ); } return arrStr.join ( "" ); } // the console.log (myCap ( 'the quick brown fox ddd')); // 7, a write function to find the longest string passed word example: input findLongest ( 'Web Development Tutorial') ; return Development function findLongest (STR) { var arrFind = str.split ( " "); // convert string array var len = arrFind.length; var maxLen =" "; // define variables acceptable maximum, the first item in the array is initially // each traversed array, for Comparative size of length for ( var I = 0; I <len; I ++ ) { IF (arrFind [I] .length> maxLen.length) maxLen = arrFind [I]; } return maxLen; } the console.log (findLongest ('The Tutorial the Web Development F' )); // . 8, the wrapper function implemented method toUpperCase example: Input myUpperCase ( "hello"); return HEllo charCodeAt () method function myUpperCase (STR) { var len = str.length; var arrStr = str.split ( "" ); var ARR = []; for ( var J = 0; J <len; J ++ ) { IF (arrStr [J] .charCodeAt ()> = 97 && arrStr [J] .charCodeAt () <= 122) { // If lowercase, enter the determination ARR [J] = the String.fromCharCode (arrStr [J] .charCodeAt () - 32); // lowercase to uppercase } The else { // if not lowercase ARR [J] = arrStr [J]; // return itself } } return arr.join ( "" ); } the console.log (myUpperCase ( "the Hello" )); // . 9, method (refer to a bubble sort), for example, sort the packages by an array function: input mySort ([2,33,41,10,5]); returns [2,5,10,33,41] function mysort (ARR) { var = len arr.length; for ( var I = 0; I <len; I ++ ) { for ( var J = 0; J <len -. 1; J ++) { Var Basket; // define a basket, to facilitate interchange two values IF (ARR [J]> ARR [J +. 1 ]) { Basket = ARR [J]; ARR [J] = ARR [J +. 1 ] ; ARR [J +. 1] = Basket; } } } return ARR; } the console.log (mysort ([ 2, 33 is, 41 is, 10,. 5 ])); // 10, the wrapper function repeatedly output string. For example: Enter repeat ( "abc", -2) ; return abc; input repeat ( "abc", 3) ; return ABCABCABC; input repeat ( "abc", 2) ; return ABCABC functionREPEAT (STR, num) { var str1 = ""; // result receiving traversal defined variable // 1, num value determination, the process returns itself is equal to zero or a negative IF (num <= 0 ) { return STR; } the else IF (num> 0) { // positive traversal num himself superimposed over for ( var I = 0; I <= num -. 1; I ++ ) { str1 + = STR; } return str1; } the else { // otherwise returns its own return NUM; } } console.log(repeat("abc", 6)) // </script> </body> </html>
These are the string method I practice written, there are many sophisticated needs and modify the contents of welcome that, thank you! ! !