Skip to content

Instantly share code, notes, and snippets.

@samocodes
Created June 23, 2024 03:31
Show Gist options
  • Save samocodes/58a7ac1010068c87bdd3c9222a9ff101 to your computer and use it in GitHub Desktop.
Save samocodes/58a7ac1010068c87bdd3c9222a9ff101 to your computer and use it in GitHub Desktop.
function revWithReduce(str: string): string {
return str.split("").reduce((acc, curr) => curr + acc, "");
}
function revWithLoop(str: string): string {
let reversed = "";
for (let i = str.length - 1; i >= 0; i--) {
reversed += str.at(i);
}
return reversed;
}
function revWithRecursion(str: string): string {
if (!str) return "";
return rev(str.substring(1)) + str.charAt(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment