Skip to content

Instantly share code, notes, and snippets.

@Giagnus64
Created January 20, 2020 18:44
Show Gist options
  • Select an option

  • Save Giagnus64/76e2a18cd63b81b3e6422e1a577be7b0 to your computer and use it in GitHub Desktop.

Select an option

Save Giagnus64/76e2a18cd63b81b3e6422e1a577be7b0 to your computer and use it in GitHub Desktop.
Reverse Broken Down
function reverse(str){
// if we arrive at the final character of the string, return that character
if (str.length === 1) return str[0];
//otherwise return the last character of the string, plus the reverse function called on a sliced version of that string
return str[str.length - 1] + reverse(str.slice(0, str.length - 1))
}
//1st Call returns "r" + reverse("ba")
//2nd call "a" + reverse("b")
//3rd Call "b"
//returns "r" + "a" + "b"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment