Skip to content

String reverse (recursion)

TypeScript recursion

Flowchart (ISO 5807)

YesNoStartInput s|s| ≤ 1Return sEndReturn reverse(s.substring(1)) + s[0]EndFigure 1 — reverse

Source code

TypeScript
function reverse(s: string): string {
    if (s.length <= 1) {
        return s;
    }
    return reverse(s.substring(1)) + s[0];
}

The editor opens with this code — change it and the chart redraws.

Language
TypeScript
Constructs
recursion
Topic
Recursion
Formats
Word, Visio, draw.io, PDF, SVG

Related examples