1// hey buddy, here are different implementations, choose your favourite !!
2
3
4// best implementation
5const reverse = (str) => str.split("").reverse().join("");
6
7
8// explanation
9function reverse1(str) {
10 // turn string to array, each char of the string will be part of the
11 // array as an individual element
12 const arr = str.split("");
13
14 // swap all the indexes of the array
15 arr.reverse();
16
17 // join every element back together
18 const res = arr.join("");
19 return res;
20}
21
22
23// without using REVERSE method
24// recursive function
25function reverse2(str) {
26 let res = "";
27 for (let i = 0; i < str.length; i++) res = str[i] + res;
28 return res;
29}
30
31
32// using REDUCE ( high order array function )
33function reverse3(str) {
34 return str.split("").reduce((output, char) => {
35 return char + output;
36 }, "");
37}
1function reverseString(str) {
2 return str.split("").reverse().join("");
3}
4reverseString("hello");
1function reverse (word) {
2 word = word.split('.').reverse().join('.')
3 return word
4
5}
6word = 'i.like.this.program.very.much'
7word = 'pqr.mno'
8console.log(reverse(word))
9
1function reverseWords(str) {
2 const allWords = str.split(" ")
3 return allWords.map(item => item.split("").reverse().join("")).join(" ")
4}
1function reverseInPlace(str) {
2 var words = [];
3 words = str.match(/\S+/g);
4 var result = "";
5 for (var i = 0; i < words.length; i++) {
6 result += words[i].split('').reverse().join('') + " ";
7 }
8 return result
9}
10console.log(reverseInPlace("abd fhe kdj"))