Characters and arrays in JavaScript some basic algorithm questions

Source: Internet
Author: User

1. Flip string

Example (requirements: first convert the string into an array, and then use the array of reverse method to flip the sequence of arrays, and finally convert the array into a string) function reversestring (str) {  str=str.split (  "). Reverse (). Join (');   return str;} ReverseString ("hello");
Split method converts a string to an array
Reverse method Flipping Array order
joinmethod to convert the array into a string

2. Calculate the factorial of an integer

 example (if the letter N represents an integer, the factorial represents the product of all integers less than or equal to N.) Factorial is usually abbreviated as n!. ; For example: 5 ! = 1  * 2  * 3  * 4  * 5  = 120   Requires:  factorialize (0)    should return 1. 
function factorialize (num) { if (Num<1 ) { return 1 ; else { return num*factorialize (num-1 ) ; }}factorialize ( 5 );

3, if the given string is a palindrome, return true , reverse, return false .

If a string ignores punctuation, capitalization, and whitespace, it is exactly the same as reading and reading back, then the string is Palindrome (palindrome). Note that you need to remove extra punctuation and whitespace from the string, and then convert the string to lowercase to verify that the string is a palindrome. The value of the function parameter can be"Racecar","Racecar"And"Race CAR". function Palindrome (str) {ASTR=str.replace (/[^0-9a-za-z]/g,"'). toLowerCase (); BSTR=astr.split (""). Reverse (). Join (""); if(astr===str) {    return true; }Else{    return false; }}palindrome (" Eye");

The regular expression can also be:/8
Astr=str.replace (/[\ |\~|\ ' |\!| \@|\#|\$|\%|\^|\&|\*|\ (|\) |\-|\_|\+|\=|\| | \\|\[|\]|\{|\}|\;|\:|\ "|\ ' |\,|\<|\.| \>|\/|\?] /g, ""). toLowerCase ();

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

Note: The return value of the function should be a number.

function Findlongestword (str) {//Conversion Group  varAstr=str.split (" " ); //The string length of each element in an array is compared by the length of the string, arranged in order of size from large to small.   varBstr=Astr.sort (function (b) {returnb.length-a.length; });//Take out the first element in the array (that is, the maximum length of the string)  varlenmax= bstr[0].length;//return Length value  returnLenmax;} Findlongestword ("The quick brown fox jumped over the lazy dog");

Characters and arrays in JavaScript some basic algorithm questions

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.