Start learning free

LESSON 373 OF 414 · PROJECTS: COMMAND-LINE TOOLS

CLI Tool: cat v1 — Print Lines in TypeScript

Practice

Build a tiny version of the Unix cat command. Given a list of lines, return them joined by newline characters (\n) so they print one per line.

Lines can be blank, and the list can be empty (the result is then an empty string).

Function signature:

cat(lines: string[]): string

Example:

Input: lines = ["hello", "world", "foo"]

Output: "hello\nworld\nfoo"

When printed:
hello
world
foo

Think of the output as one big string where each input line is separated from the next by a newline character. TypeScript arrays have a built-in method that glues all elements together with a chosen separator.

Call lines.join("\n") — that is the array join method with "\n" as the separator.

The full solution is a one-liner:
class Solution {
    cat(lines: string[]): string {
        return lines.join("\n");
    }
}

STEP 1 OF 1
    Ctrl + Enter to check

    Cat V1 in TypeScript

    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free