算法要求

``````function reverseString(str) {
return str;
}
reverseString("hello");``````

提供测试用例

• reverseString(“hello”)  应该返回 “olleh”
• reverseString(“Howdy”)  应该返回 “ydwoH”
• reverseString(“Greetings from Earth”)  应该返回 ”htraE morf sgniteerG”

1. 使用内置方法反转字符串

• split() 方法使用指定的分隔符字符串将一个 String 对象分割成子字符串数组，以一个指定的分割字串来决定每个拆分的位置
• reverse() 方法将数组中元素的位置颠倒，并返回该数组。数组的第一个元素会变成最后一个，数组的最后一个元素变成第一个。该方法会改变原数组
• join() 方法将一个数组（或一个类数组对象）的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个项目，那么将返回该项目而不使用分隔符
``````function reverseString(str) {
// Step 1. Use the split() method to return a new array
var splitString = str.split(""); // var splitString = "hello".split("");
// ["h", "e", "l", "l", "o"]

// Step 2. Use the reverse() method to reverse the new created array
var reverseArray = splitString.reverse(); // var reverseArray = ["h", "e", "l", "l", "o"].reverse();
// ["o", "l", "l", "e", "h"]

// Step 3. Use the join() method to join all elements of the array into a string
var joinArray = reverseArray.join(""); // var joinArray = ["o", "l", "l", "e", "h"].join("");
// "olleh"

//Step 4. Return the reversed string
return joinArray; // "olleh"
}

reverseString("hello");``````

三个方法组合形成链式调用:

``````function reverseString(str) {
return str.split("").reverse().join("");
}
reverseString("hello");``````

2. 用递减的 for 循环反转字符串

``````function reverseString(str) {
// Step 1. Create an empty string that will host the new created string
var newString = "";

// Step 2. Create the FOR loop
/* The starting point of the loop will be (str.length - 1) which corresponds to the
last character of the string, "o"
As long as i is greater than or equals 0, the loop will go on
We decrement i after each iteration */
for (var i = str.length - 1; i >= 0; i--) {
newString += str[i]; // or newString = newString + str[i];
}
/* Here hello's length equals 5
For each iteration: i = str.length - 1 and newString = newString + str[i]
First iteration:    i = 5 - 1 = 4,         newString = "" + "o" = "o"
Second iteration:   i = 4 - 1 = 3,         newString = "o" + "l" = "ol"
Third iteration:    i = 3 - 1 = 2,         newString = "ol" + "l" = "oll"
Fourth iteration:   i = 2 - 1 = 1,         newString = "oll" + "e" = "olle"
Fifth iteration:    i = 1 - 1 = 0,         newString = "olle" + "h" = "olleh"
End of the FOR Loop*/

// Step 3. Return the reversed string
return newString; // "olleh"
}

reverseString('hello');``````

删除注释:

``````function reverseString(str) {
var newString = "";
for (var i = str.length - 1; i >= 0; i--) {
newString += str[i];
}
return newString;
}
reverseString('hello');``````

3. 用递归反转字符串

• substr() 方法返回一个字符串中从指定位置开始到指定字符数的字符.

``````'hello'.substr(1); // "ello"
``````
• charAt() 方法从一个字符串中返回指定的字符.
``````'hello'.charAt(0); // "h"
``````

``````function reverseString(str) {
if (str === "") // 如果传入空字符串，则直接返回它
return "";
else
return reverseString(str.substr(1)) + str.charAt(0);
/*

1st call – reverseString("Hello")   will return   reverseString("ello")           + "h"
2nd call – reverseString("ello")    will return   reverseString("llo")            + "e"
3rd call – reverseString("llo")     will return   reverseString("lo")             + "l"
4th call – reverseString("lo")      will return   reverseString("o")              + "l"
5th call – reverseString("o")       will return   reverseString("")               + "o"

``````

删除注释:

``````function reverseString(str) {
if (str === '') return '';
else return reverseString(str.substr(1)) + str.charAt(0);
}
reverseString('hello');
``````

使用三元表达式:

``````function reverseString(str) {
return str === '' ? '' : reverseString(str.substr(1)) + str.charAt(0);
}
reverseString('hello');
``````

JavaScript 字符串反转  是一种小型且简单的算法，在电话技术筛查或技术面试中都可能被问到。 你可以采用最简单的方式解决此问题，也可以采用递归或更复杂的解决方案来解决。

＃保持好奇，＃持续前进和＃实现目标！