Some basic exercises of javascript

Source: Internet
Author: User

One, flip string

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 arrays into strings.

1 function reversestring (str) {2   var array = []; 3   Array =  str.split (""). Reverse (). Join (""); 4   return Array; 5 }67 reversestring ("Hello");

Second, calculate the factorial of an integer

If the letter n is used to represent an integer, the factorial represents the product of all integers less than or equal to N.

function factorialize (num) {  if (num<=1) {  return 1;  } Else   {  return num*factorialize (num-1);  }} Factorialize (5);

Third, if the given string is a palindrome, returns true , conversely, returns 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).

function palindrome (str) {  var re =/[\w_]/g;    var lowregstr = Str.tolowercase (). replace (Re, ');    var reversestr = Lowregstr.split ("). Reverse (). Join (');    return Reversestr = = = Lowregstr;} Palindrome ("eye");

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

 function   Findlongestword (str) {  Span style= "COLOR: #0000ff" >var  arr = []; Arr  = Str.split (""         var  max=0;         var  t=0;  for  (var  i = 0; i < arr.length; i++<            Span style= "color: #000000") {t  =arr[i].length;  if  (T>max) {max  = T;  }}  return   Max; }findlongestword ( The quick brown fox jumped over the lazy dog); 

Five, make sure that the first letter of each word of the string is capitalized and the remainder is lowercase.

 function   Titlecase (str) { var  arr = Str.tolowercase (). Split (" for  (var  i = 0;i < arr.length;i++< Span style= "color: #000000") { var  char
     = Arr[i].charat (0 =arr[i].replace (char , function  replace (char   return  char  .touppercase ();});  return  arr.join (" "I m a Little tea pot"); 

The large array on the right contains 4 decimal groups, finds the maximum in each decimal group, and then concatenates them together to form a new array.

function Largestoffour (arr) {  var lar = [0,0,0,0];    for (var i = 0;i<arr.length;i++) {    for (var j = 0; j < arr[i].length; j++<
   
     c12>) {      
    if(lar[i]<
    arr[i][j]) {         =
     arr[i][j];   }}} 
    return 
    Lar;} Largestoffour ([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
   

Seven, check whether a string ( str ) ends with the specified string ( target ).

function confirmending (str, target) {  if (Str.substr (str.length-target.length, str.length) = = =  Target) {     returntrue;    } Else    {        returnfalse;    }} Confirmending ("Bastian", "n");

Eight, repeats a specified string num , and num returns an empty string if it is a negative number.

function repeat (str, num) {  var astr=str;   if (num<0) {    return "";  } Else {    for (var i=0;i<num-1;i++) {      str+ =astr     ;    }  return str;  }} Repeat ("abc", 3);

  

Some basic exercises of javascript

Related Article

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.