Task: Reverse the provided string in as many different ways as possible.
Tests: All the solutions should pass the following tests:
"hello"
should return a string."hello"
should become"olleh"
."Howdy"
should become"ydwoH"
."Greetings from Earth"
should return"htraE morf sgniteerG"
Code:
When I first came across this challenge on freecodecamp.org, this is the solution I came up with. It's pretty self-explanatory
function oneLinerReverse(str) { return str.split('').reverse().join(''); }
Ah, the humble loop. It is not the cleanest way but it still gets the job done. To avoid using the .reverse() array method, we use the length of the split string and the count to re-assign the letters in reverse order and then join them
function forLoopOverString(str) {
let strArr = []
let newStr = str.split('');
let count = 0;
strArr.length = newStr.length
for (count; count < newStr.length; count++) {
strArr[newStr.length - count] = newStr[count]
}
return strArr.join('')
}
Using charAt(), we can avoid doing any splitting and just simply target the specific characters we want to re-assign
function forLoopCharAt(str) {
let strLen = str.length;
let count = 0;
let newStr = []
for (count; count < strLen; count++) {
newStr[strLen - count] = str.charAt(count)
}
return newStr.join('')
}
This method introduces the forEach loop, which is better than the for loop because we don't have to keep a count. We know this function will run for every item in the array. The pop() method returns the last element in the array so each time we do a loop, we just simply pop away.
function forEachPushPop(str) {
let strArr = str.split('');
let tempArr = [];
str.split('').forEach(el => tempArr.push(strArr.pop()));
return tempArr.join('');
}
I tried doing this myself for a bit and struggled, so I looked online to see how others have done it. Why does this work? Recursion creates a series of nested functions, so when the recursion is finished, the functions 'unwind' starting with the most inner nested function.
function recursiveReverse(str) {
if (str === '') return ''; // base and terminal case
return recursiveReverse(str.substr(1)) + str[0]; // the actual recursion
}
What happens if you take the number two solution and replace it with a while loop?
function whileLooping(str) {
let strArr = []
let newStr = str.split('');
let count = 0;
strArr.length = newStr.length
while (count < newStr.length) {
strArr[newStr.length - count] = newStr[count]
count++
}
return strArr.join('')
}
And if you use a do...while loop?
function doWhileLooping(str) {
let strArr = []
let newStr = str.split('');
let count = 0;
strArr.length = newStr.length
while (count < newStr.length) {
strArr[newStr.length - count] = newStr[count]
count++
}
return strArr.join('')
}
Use a for loop but instead of counting up, count down. Also, no join('')! Taken from this
function doWhileLooping(str) {
var newStr = "";
for (var i = str.length - 1; i >= 0; i--) newStr += str[i];
return newStr;
}
Strings are iterable data structures, so we can use the new for..of
loop. See this for more on for..of
. I learnt this one here
function forOffin(str) {
let reversedStr = "";
for (let char of str) {
reversedStr = `char${reversedStr}`
}
return reversedStr;
}
Instead of using split('') to create an array from the string, we can spread the string characters into an array and then hand the array over to reverse() and join(''). I learnt this one here
function oneLineSpread(str) {
return [..str].reverse().join('');
}
When I first began this, I had a feeling you could use the reduce() method but I could not work it out until I came across this article.
function reducedAndReversed(str) {
return str.split('').reduce((rev, char) => char + rev, '');
}