JavaScript notes (ii)--application of common arrays and string methods

Source: Internet
Author: User
Tags string methods

1. Flip the characters in the string, such as ' Hello ', and flip it into ' Olleh '.

var arr=[];

function ReverseString (str) {
Arr=str.split ("");

Arr=arr.reverse ();

Str=arr.join ("");
return str;
}

ReverseString ("Hello");

2. Calculate the factorial of an integer

function factorialize (num) {
if (num<=1) {
return 1;
}
else if (num>0) {
Return Factorialize (num-1) *num;
}

return num;
}

Factorialize (5);

3. Detects if a string is a palindrome number, or returns TRUE.

A palindrome is a word or sentence that has been read backwards and forwards, with the exception of punctuation marks and spaces, and special symbols.

var arr=[];
var str1= "";

function Palindrome (str) {

Str=str.replace (/[^0-9a-za-z]/g,function () {
Return "";
});
Str=str.tolowercase ();

Arr=str.split ("");
Arr=arr.reverse ();

Str1=arr.join ("");

if (STR1===STR) {
return true;
}
else{
return false;
}

}
Palindrome ("Eye");

4. Find the longest word in a string and return its length.

function Findlongestword (str) {
var arr=[];
var arr1=[];
var num=0;
Arr=str.split ("");
for (i=0;i<arr.length;i++) {

Arr1.push (arr[i].length);

}

Num=math.max.apply (NULL,ARR1);
return num;
}
Findlongestword ("May, the force is with You");
5. Capitalize the first letter of each word in the string, with the other letters lowercase
var arr=[];
function Titlecase (str) {
Str=str.tolowercase ();
Arr=str.split ("");
for (i=0;i<=arr.length;i++) {
Arr[i].charat (0). toUpperCase ();
}
Str=arr.join ("");
return str;
}

Titlecase ("I ' m a Little tea pot");
6. Find the largest number of each element in a two-dimensional array, and put those numbers in a new array as the function return value.

function Largestoffour (arr) {

var num=0;
var arr1=[];
for (i=0;i<arr.length;i++) {

Num=math.max.apply (Null,arr[i]);
Arr1.push (num);

}

return arr1;
}
Largestoffour ([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);

7. Write a function that detects whether the end of a string is a given character, the first argument is a string, and the second argument is the character to be matched to its end. You cannot use the Endwidth () method.

function confirmending (str, target) {

var str1=[];
var num=target.length;
Str1=str.substr (Str.length-num,num);
if (str1==target) {
return true;
}
else{
return false;
}
}
Confirmending ("Bastian", "n");

JavaScript notes (ii)--application of common arrays and string methods

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.