Summary of the FCC's JS basic algorithm 1

Source: Internet
Author: User

1. Flipping a string

Solution: First convert the string into an array, then use the reverse method of the array to flip the order of the arrays, and finally convert the array into strings.

1 function reversestring (str) {2   var arr = [];    3   arr = Str.split (""); 4   arr.reverse (); 5   str = Arr.join (""); 6   return str; 7 }89 reversestring ("Hello");

It is mainly used for string.split (), Array.reverse (), Array.join ().

2. Calculate the factorial of an integer

Solution: F (n) =n*f (n-1)

1 functionfactorialize (num) {2   if(Num <= 1) {3     return1;4}Else {5num = num * Factorialize (num-1);6   }7   returnnum;8 }9 TenFactorialize (5);

This is a recursive function, in the study of "JS Advanced Programming" recursive function discovery, such as the code will be shallow copy and put factorialize=null error, so apply Arguments.callee point to the executing pointer

1 function factorialize (num) {2   if (Num <= 1) {3     return 1; 4   Else {5     num = num * Arguments.callee (num-1); 6   }7   return  num; 8 }

However, in strict mode, you cannot access Arguments.callee. Again improved as follows

1 var factorialize = (function  f (num) {2   if(num <= 1) {  3     return 1; 4   Else {5     num = num * F (num-1); 6   }7   return  num; 8 }
3. Check if the string is Palindrome

Workaround: First ignore the string punctuation, capitalization, and space, then decompose the string into an array and then invert it into a new string, comparing the two strings before and after

1 functionpalindrome (str) {2   //Good luck!3str = str.replace (/\w+/g, "");4str = str.replace (/\_+/g, "");5str =str.tolowercase ();6   vararr = Str.split ("");7arr =arr.reverse ();8   varSTR2 = Arr.join ("");9   if(str = =str2) {Ten     return true; One}Else { A     return false; -   } - } thePalindrome ("Almostomla");

4. Find the longest word in the provided sentence and calculate its length.

Workaround: Iterate to compare word lengths

1 functionFindlongestword (str) {2   vararr = Str.split ("");3   varARR2 = [];4   varA = 0;5    for(vari = 0; i < arr.length; i++) {6     varb =arr[i].length;7     if(B >a) {8A =b;9     }Ten } One   returnA; A } -Findlongestword ("What if we try a super-long word such as otorhinolaryngology");

5. The first letter of each word in the string is capitalized, the remainder is lowercase

Solution: All words are first converted to lowercase, and then the first letter of each word is iterated instead

1 functiontitlecase (str) {2str =str.tolowercase ();3   vararr = Str.split ("");4   varSTR2 = "";5   varARR2 = [];6    for(vari = 0; i < arr.length; i++) {7     varA = Arr[i][0].touppercase ();8     varSTR1 = Arr[i].replace (/^\w/, a);9 Arr2.push (str1);Ten   } OneSTR2 = Arr2.join (""); A   returnstr2; - } -  theTitlecase ("I ' m a Little tea pot");

6. The large array contains 4 decimal groups, each of which finds the maximum value in each decimal group, and then concatenates them to form a new array

Workaround: The first method, iterate to find the maximum value

function Largestoffour (arr) {
var arr1 = []; for (var i = 0; i < arr.length; i++) { var a = 0; for (var j = 0; J < Arr[i].length; J + +) { if(Arr[i][j] > a ) {= arr[i][j]; } } Arr1.push (a); } return arr1;}

The second method, the direct use of Math.max ()

1 functionLargestoffour (arr) {2 varARR2 = [];3    for(vari = 0; i < arr.length; i++) {4     varARR1 =Arr[i];5     varA = Math.max.apply (NULL, arr1);6 Arr2.push (a);7   }8   returnarr2;9}
7. Check whether a string ends with the specified string
1 functionconfirmending (str, target) {2   varA =target.length;3   varb = Str.substr (-a);4   if(b = =target) {5     return true;6}Else {7     return false;8   }9 }Ten  OneConfirmending ("Bastian", "n");
8. Repeat a specified string of n times
1 functionrepeat (str, num) {2   vararr = [];3    for(vari= 0; i < num; i++) {4Arr[i] =str;5   }6str = Arr.join ("");7   returnstr;8 }9 TenRepeat ("ABC", 3);

Summary of the FCC's JS basic algorithm 1

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.