-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseString.js
More file actions
27 lines (21 loc) · 885 Bytes
/
reverseString.js
File metadata and controls
27 lines (21 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//! Method 1: use array.reverse() method and string.join() method
//var name = "rakib";
const reverseString = (string) => [...string].reverse().join("");
console.log(reverseString("Rakib"));
//! Method 2 Reverse a String With a Decrementing For Loop
const reString = (str) => {
var newString = "";
for (var i = str.length - 1; i >= 0; i--) {
newString += str[i];
}
return newString;
};
console.log(reString("healer"));
//! Method 3: Reverse a String With Recursion
function reverseString2(str) {
if (str === "") return "";
else return reverseString2(str.substr(1)) + str.charAt(0);
//? The substr() method returns the characters in a string beginning at the specified location through the specified number of characters.
//? The charAt() method returns the specified character from a string.
}
console.log(reverseString2("hello"));