JS primary scripting algorithm

Source: Internet
Author: User
Tags first string truncated

Original link

Flip String Algorithm Challenge
function reverseString(str){    str = str.split(‘‘).reverse().join(‘‘); return str;}reverseString("hello");

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

Factorial algorithm
function factorialize(num){    var b = 1; for( var i=1;i<=num;i++){ b = i * b; } return b;}facrorialize(5);

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

Palindrome algorithm
function Palindrome (str) {str = str.replace (/[\ |\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\ (| \)| \-| \_| \+| \=| \| | \\| \[| \]| \{| \}| \;|\:|\ "| \ ' | \,| \<| \.| \>| \/| \? /g, ""). toLowerCase (); var len = Math.floor (STR.LENGTH/2); for (Var i=0;i<len;i++) {if (Str[i]! = Str[str.length-1]) {return false;}} return true;} Palindrome ("Race Car");               

If a string ignores punctuation, capitalization, and whitespace, it is exactly the same as reading and inverting, then the string is Palindrome (palindrome), returns True, and returns false;

Note: 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

Finding the longest Word algorithm challenge
function findLongestWord(str){    var max = 0;    var len = str.length; for(var i = 0; i < len; i++){ var length = str[i].length; if( length > max){ max = str[i].length } } return max;}findLongestWord("The quick brown fox jumped over the lazy dog");
Set the initial capitalization algorithm
function Titlecase (str) { str = str.tolowercase ().  Split ('); var len = str.length; For (var i = 0; i < len; i++) { Str[i] = Str[i].slice (0,1). toUpperCase () + Str[i].slice (
                 
                  1); }; 
                  str = str.join (') return str; Titlecase ("I ' m a Little tea pot");       
                       

Make sure that the first letter of each word in the string is capitalized, the remainder lowercase

Finding the maximum algorithm in an array
function Largestoffour (arr) {var len = arr.LengthVararray = [];Forvar i =0; i < Len; i++) {Length = Arr[i].LengthVarMax =0;Forvar j =0; J <Length J + +) {if (ARR[I][J) >max) {max = Arr[i][j];}} array. push (max); max = 0;}; return array;} Largestoffour ([[4, 5, 1, 3], [13, 27, 18, Span class= "Hljs-number" >26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);     

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

Use the For loop to iterate through the algebraic group and access each element of the array by means of arr[i].

Confirm End Character algorithm
function confirmEnding(str,target){     if(str.endWidth(target)){ return true; }else{ return false; } }confirmEnding("Bastian", "n");

Checks whether a string (str) ends with the specified string (target). If yes, returns true if not, returns false

Repetitive operation algorithm
function repeat(str,num){       var string = ‘‘; for(var i = 0; i <= num; i++){ string = string + str; } return string;}confirmEnding("abc", "5");

Repeats a specified string num times, and returns an empty string if num is a negative number

String interception algorithm
functionTruncate (STR,NUM) {var len = str.LengthIfNum <=3) {if (Len >num) {str = str.Substring0,num) + ' ... '; }else{str = str.substring (0,num);}} else if (num > 3" {if (length > num) {str = Str.substring (0,num-< Span class= "Hljs-number" >3) + ' ... '; }else{str = str.substring (0,< Span class= "hljs-built_in" >num); }} return str;} truncate ( "A-tisket a-tasket A Green and yellow basket", 11)              

Truncate a String! If the length of the string is longer than the specified parameter num, the extra part is used ... To express. Remember that the three-point number inserted at the end of a string also accounts for the length of the string. However, if the specified parameter num is less than or equal to 3, the added three dot number is not counted in the length of the string

Array segmentation algorithm
function chunk(arr,size){       var arr1 = []; for(var i = 0; i < arr.length; i+=size){ var brr = arr.slice(i,i+size); arr1.push(brr); } return arr11;}chunk(["a", "b", "c", "d"], 2);

Divides an array of arr into a number of array blocks, as specified by the size of the arrays.

For example: Chunk ([1,2,3,4],2) =[[1,2],[3,4]];

Chunk ([1,2,3,4,5],2) =[[1,2],[3,4],[5]];

Array truncation algorithm
function slasher(arr, howMany) {    var len = arr.length; if( howMany > length ){ arr = []; }else{ arr = arr.slice(howMany); } return arr;}slasher([1, 2, 3], 2);

Returns the remaining elements after an array is truncated by n elements, truncated starting at index 0

Array query algorithm
function mutation(arr) {    var str = arr[1].toLowerCase(); var len = str.length; for(var i = 0;i < len;i++){ if( arr[0].toLowerCase().indexOf(str[i])<0 ){ return false; } } return true;}mutation(["hello", "heo"]);

The function returns True if the first string element of the array contains all the characters of the second string element.

For example, ["Hello", "hello"] should return true because all the characters of the second string can be found in the first string if the case is ignored.

["Hello", "hey"] should return false because the string "hello" does not contain the character "Y".

Delete a specific value algorithm in an array
function bouncer(arr) {    var len = arr.length; for(var i=len-1;i>=0;i--){ if( Boolean(arr[i]) == false ){ arr.splice(i,1); } } return arr;}bouncer([7, "ate", "", false, 9]);

Deletes all false values in the array. In JavaScript, false values are false, NULL, 0, "", Undefined, and NaN

Remove any number of value algorithms in an array
functionDestroyer ( arr) {var len = arr.length; var length = arguments.length; for (var i = 1; i<length;i++) { Span class= "Hljs-keyword" >for (var j=len-1;j>=0;j--) {if (arr[j] = = arguments[i]) { Arr.splice (J,1);}} } return arr;} Destroyer ([1, 2, 3, 1, 2, 3], 2, 3);               

Implement a Destroy (destroyer) function, the first parameter is the array to be destroyed, the remaining parameters are the values to be destroyed

Array sorting and inserting value algorithm
 function where (arr, num) {arr.    sort ();    var len = Arr.length; for (var i=0;i<len;i++) { Span class= "Hljs-keyword" >if (Arr[i] > num) {arr.splice ( I,0, num); return arr;} } arr.push (num); return arr;} where ([2,7,4, 5], 50);         

Sort the array first, then find the specified value in the position of the array, and finally return the index of the position corresponding

Example: where ([1,2,3,4], 1.5) should return 1. Since 1.5 is inserted into the array [1,2,3,4] and becomes [1,1.5,2,3,4], the 1.5 corresponding index value is 1.

Displacement cipher algorithm
function rot13 (STR) {var arr = []; var len =Str.length;str =Str.touppercase ();for (Var i=0;i<len;i++) {IfStr[i].charcodeat () >=&&Str[i].charcodeat () <=77) {var num = str[i].charcodeat () +13;} else if (str[i].charCodeAt () >77 && str[i].charcodeat () <=90" {var num = str[i].charcodeat () -13;} else{num = str[i];} arr.push ( String.fromCharCode (num)); } str = Arr.join (return STR;} ROT13 ( "SERR pbqr pnzc");           

This is the world's most popular Caesar password Caesar cipher, also known as the shift password. The shift password, which is the letter in the password, is shifted according to the specified number. A common case is the ROT13 password, which shifts the letters to 13 positions. By ' A '? ' N ', ' B '? ' O ', and so on.

Write a ROT13 function, implement input encryption string, output decrypt string. All letters are uppercase, do not convert any non-alphabetic characters (for example: spaces, punctuation marks), encounter these special characters, skip them

Original link

JS primary scripting algorithm

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.