Write a function called reverseString that takes in a string and returns the reverse of that string. In this section, we are really focusing on loops without using any built-in methods, so try that first. If you get stuck, you can always use the built-in methods to solve the problem.
/**
* 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') // 'olleh'
reverseString('world') // 'dlrow'
reverseString('') // ''- The input string will only contain lowercase letters and spaces
- You can also do this without using any of the built-in methods and just use a for loop.
- You could also use the methods
split,reverse, andjointo solve this problem.
Click For Solution 1
This solution uses a for loop to reverse the string.
function reverseString(str) {
let reversed = '';
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}- Create a variable called
reversedand set it equal to an empty string. - Create a for loop that starts at the last index of
strand decrements by 1 until it reaches 0. - Add the character at the current index to the
reversedvariable. - Return the
reversedvariable.
Click For Solution 2
This solution uses built-in methods to reverse the string.
function reverseString(str) {
return str.split('').reverse().join('');
}We created a function called reverseString that takes in a string called str. We then return the result of chaining the split, reverse, and join methods on str.
The split function takes in a string and turns it into an array. We passed in an empty string as an argument to split so that it will split the string into an array of characters.(["h", "e", "l", "l", "o"])
The reverse function takes in an array and reverses it. (["o", "l", "l", "e", "h"])
The join function takes in an array and turns it into a string. We passed in an empty string as an argument to join so that it will join the array of characters into a string. ('olleh')
test('Reversing a string', () => {
expect(reverseString('Hello')).toBe('olleH');
expect(reverseString('JavaScript')).toBe('tpircSavaJ');
expect(reverseString('12345')).toBe('54321');
});