1function reverseString(s){
2 return s.split("").reverse().join("");
3}
4reverseString("Hello");//"olleH"
1function reverseString(str) {
2 return str.split("").reverse().join("");
3}
4reverseString("hello");
1function reverseString(str) {
2 if (str === "")
3 return "";
4 else
5 return reverseString(str.substr(1)) + str.charAt(0);
6}
7reverseString("hello");
1function reverseString(str) {
2 // Step 1. Create an empty string that will host the new created string
3 var newString = "";
4
5 // Step 2. Create the FOR loop
6 /* The starting point of the loop will be (str.length - 1) which corresponds to the
7 last character of the string, "o"
8 As long as i is greater than or equals 0, the loop will go on
9 We decrement i after each iteration */
10 for (var i = str.length - 1; i >= 0; i--) {
11 newString += str[i]; // or newString = newString + str[i];
12 }
13 /* Here hello's length equals 5
14 For each iteration: i = str.length - 1 and newString = newString + str[i]
15 First iteration: i = 5 - 1 = 4, newString = "" + "o" = "o"
16 Second iteration: i = 4 - 1 = 3, newString = "o" + "l" = "ol"
17 Third iteration: i = 3 - 1 = 2, newString = "ol" + "l" = "oll"
18 Fourth iteration: i = 2 - 1 = 1, newString = "oll" + "e" = "olle"
19 Fifth iteration: i = 1 - 1 = 0, newString = "olle" + "h" = "olleh"
20 End of the FOR Loop*/
21
22 // Step 3. Return the reversed string
23 return newString; // "olleh"
24}
25
26reverseString('hello');