Reverse a String: How this code work?

Tell us what’s happening:

Your code so far


function reverseString(str) {
   return (str === '') ? '' : reverseString(str.substr(1)) + str.charAt(0);
}

reverseString("hello");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/79.0.108 Chrome/73.0.3683.108 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/reverse-a-string/

i mean this part: return (str === ‘’) ? ‘’ : reverseString(str.substr(1)) + str.charAt(0);

i don’t understand when (str === ‘’)

Hello @tuanldhe140929,

Per the challenge instructions, you will need to split the string into array, reverse the array, then join the array to complete this challenge. You don’t need a conditional statement to complete this challenge.

1 Like