[igeek manual] daily algorithm 1 - string flip

Title : Flip a String

Target:

'hello' ——> 'olleh'

Ideas:

Convert the string into an array, and then use the reverse method of the array to reverse the order of the array, and then convert the array into a string.

Code:

function reverseString(str) {
    str = str.split('').reverse().join('')
    return str;
}

reverseString("hello");     // olleh

expand:

Common string methods
 charAt (num)          // Get the single character at the specified index position 
charCodeAt (num)      // Get the Unicode value of the character at the specified index position (ascii is a subset) 
concat (str01,str02)  // Concatenate two characters ~ 
indexOf ( "str" ​​)       // take the index of the first occurrence of str 
lastIndexOf ( "str" ​​)   // take the index of the last occurrence of str 
replace ( "oldStr" , "newStr" )  // find oldStr and replace it with newStr 
slice ( start , end )  // The object can be a string or an array, remember that the range does not include end 
substr (start, length) // length characters are taken from the index start, length cannot be less than 0, otherwise an empty string is returned


Common array methods
pop ()          // delete last item 
shift ()        // delete first item 
push ()         // add to last 
unshift ()      // add to first 
indexOf ()      // array element index 
slice ()        // intercept (slice ) array to get the intercepted array 
concat ()       // array merge 
reverse ()      // array flip

 


 

Write it after:

    This series of courses mainly starts with the most basic algorithms. Every day, a small and beautiful algorithm is presented, and some common knowledge points are explained together to expand what has been learned and learn by analogy. Aiming to make every exquisite side dish. Here is igeekbar , I am the geek killer of iGeekBar~ What 's your problem! Feel free to message me anytime~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326388775&siteId=291194637