forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol1.js
30 lines (23 loc) · 857 Bytes
/
sol1.js
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
28
29
30
/**
* @author MadhavBahlMD
* @date 21/12/2018
*/
// Step 1: Strings are immutable (in JavaScript), hence convert it into array.
// Step 2: Run a loop from 0 to (size of array)/2 and interchange the element at i index with element at position size-1-i
// Step 3: Join the array to form a string
function strRev (str) {
var i, j, temp, len = str.length;
// Strings are immutable in javascript, hence we convert the string into an array
str = str.split('');
// Reverse the array elements by interchanging the elements which are equidistant from start and end
for (i=0, j=len-1; i<=len/2; i++, j--) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
// Join the reversed array into a string
str = str.join('');
// Return the reversed string
return str;
}
console.log(strRev('Hello!'));