Write a function called reverseString
that takes in a string and returns the reversed version of the string. Be sure to use recursion in your solution.
/**
* Returns the reverse of a string.
* @param {string} str - The string to reverse.
* @returns {string} - The reverse of the string.
*/
function reverseString(str: string): string;
reverseString('hello'); // should return 'olleh'
reverseString('world'); // should return 'dlrow'
reverseString(''); // should return ''
reverseString('a'); // should return 'a'
reverseString('racecar'); // should return 'racecar'
- As a base case, you can check if the string is empty and return an empty string if so.
- You can use recursion to reverse the string by recursively calling the function with the substring starting from the second character and then concatenating the first character at the end.
- Remember how unwinding works and how function returns are added to the call stack in the reverse order of the function calls.
Click For Solution
function reverseString(str) {
if (str === '') {
return '';
}
return reverseString(str.substr(1)) + str[0];
}
The reverseString
function uses recursion to reverse the string.
- If the input string is empty (
str === ''
), return an empty string (''
). Otherwise, it calls itself with the substring starting from the second character (str.substr(1)
) and concatenates the first character of the original string at the end (str[0]
).
For example, if the input is 'hello'
, the function first calls itself with 'ello'
and concatenates 'h'
at the end. Then it calls itself with 'llo'
and concatenates 'e'
at the end. This process continues until the input becomes an empty string, and then the function starts concatenating the characters in reverse order, resulting in the reversed string 'olleh'
.
It is important to have that base case of an empty string, otherwise the function will continue to call itself with substrings of the original string until it runs out of memory and crashes.
Let's break it down a little more...
- When we call reverseString('hello'), it executes reverseString('ello') + 'h'.
- Now, reverseString('ello') calls reverseString('llo') + 'e'.
- Continuing, reverseString('llo') calls reverseString('lo') + 'l'.
- In the next call, reverseString('lo') calls reverseString('o') + 'l'.
- Finally, reverseString('o') returns 'o'.
Now, we can start "unwinding" the recursion and concatenating the characters to form the reversed string:
- 'o' + 'l' gives 'ol'.
- 'ol' + 'l' gives 'oll'.
- 'oll' + 'e' gives 'olle'.
- 'olle' + 'h' gives 'olleh'.
So, the function concatenates the characters in reverse order as it "unwinds" the recursion, effectively reversing the original string.
We could even cut this solution down to a single line:
// Shorter version
const reverseString = (str) =>
str === '' ? '' : reverseString(str.substr(1)) + str.charAt(0);
test('Reversing a string', () => {
expect(reverseString('Hello')).toBe('olleH');
expect(reverseString('JavaScript')).toBe('tpircSavaJ');
expect(reverseString('12345')).toBe('54321');
});