javascriptbeginner10 minutes
Reverse a String Using a JavaScript Function
Create a function that takes a string as input and returns the string reversed. This challenge will help you understand basic string manipulation and working with array methods in JavaScript.
Challenge prompt
Write a JavaScript function named reverseString that accepts a single parameter (a string) and returns the string with its characters in reverse order. For example, if the input is 'hello', the function should return 'olleh'.
Guidance
- • Think about how you can convert the string into an array to use array methods.
- • Consider using built-in JavaScript methods like split(), reverse(), and join() for a simple solution.
- • Remember that strings are immutable, so you cannot reverse them directly.
Hints
- • Use the split('') method to convert the string into an array of characters.
- • Use the reverse() method to reverse the array elements.
- • Use the join('') method to combine the reversed array back into a string.
Starter code
function reverseString(str) {
// Your code here
}Expected output
reverseString('hello') // 'olleh' reverseString('DevDuel') // 'leuveD' reverseString('JavaScript') // 'tpircSavaJ'
Core concepts
string manipulationarray methodsfunctions
Challenge a Friend
Send this duel to someone else and see if they can solve it.