String reversal
JavaScript
Easy
Objective
Write a function that reverses a string
Requirements
The function should be named
reverseString
It should take a single string as an argument
It should return the reversed string
Check your solution
Tests
Function name is reverseString | |||
reverseString("lorem") | Expected: | "merol" | |
reverseString("a b c") | Expected: | "c b a" |
Solution
Here's how I would approach this, but keep in mind there might be other ways to solve this problem.
function reverseString(string) {
// Turn the string into an array, reverse it, then turn it back into a string
return string.split('').reverse().join('');
}
Click to reveal