Start learning free

LESSON 1 OF 70 · PROJECTS: COMMAND-LINE TOOLS

cat v1 in TypeScript: Print Lines with join

Practice Read the full explanation

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

    TYPESCRIPT · PROJECTS: COMMAND-LINE TOOLS

    Cat V1 in TypeScript, explained

    Problem overview

    The Unix cat command prints text line by line. This first version of the project is a function, cat(lines: string[]): string, that takes the lines and returns one string with a newline character between each two of them. Printing that string puts one line on each row.

    For ["hello", "world", "foo"] the result is "hello\nworld\nfoo". Blank lines are ordinary entries, and an empty list gives an empty string.

    Approach: join, not concatenation in a loop

    You could build the result by looping and adding "\n" after each line, but then the last line would end with a newline the expected output doesn't have. The key word in the task is "between": the separator goes between two lines, never after the last one.

    Arrays have a method for exactly this. array.join(separator) glues the elements into one string with the separator between each pair. It adds nothing before the first element or after the last, and an array with one element comes back as that element alone.

    Here is what the loop version produces for ["hello", "world", "foo"] if it adds "\n" after every line:

    "hello\nworld\nfoo\n"

    That has 16 characters, one more than the correct result, "hello\nworld\nfoo", which has 15. join gets it right without a special case for the last line.

    Calling join() with no argument is a different trap: the default separator is a comma, so ["hello", "world", "foo"] gives "hello,world,foo". The newline has to be passed explicitly.

    Step by step

    1. Return lines.join("\n"). That is the whole function: the separator is the newline escape "\n", and join places it between the lines.

    The solution in TypeScript

    class Solution {
        cat(lines: string[]): string {
            return lines.join("\n");
        }
    }
    

    A few details are worth knowing:

    • "\n" is a single character, the newline. In the result it is a real line break; it only looks like two characters in the source code.
    • join does not change lines. It builds and returns a new string.
    • The types string[] and string are checked by the compiler and then erased when the code becomes JavaScript. The function itself is plain JavaScript join.

    To see the result as a person would, pass it to console.log. The string "hello\nworld\nfoo" is shown as three rows:

    hello
    world
    foo

    The function only builds the string; printing is left to the caller.

    Complexity

    Time is O(total length), since every character of every line is copied into the result once. Space is the same, for the new string.

    Edge cases and common mistakes

    • Empty list. [].join("\n") is "", so no special case is needed.
    • One line. ["x"] gives "x", with no newline.
    • Blank lines. An empty string is still an entry. ["a", "", "b"] gives "a\n\nb", which prints as a blank row between a and b. A list holding only [""] gives "", the same as an empty list: one blank line has no characters to show.
    • A trailing newline. Don't add "\n" at the end. The tests expect none.

    Testing

    InputResult
    ["hello", "world", "foo"]"hello\nworld\nfoo"
    ["a", "", "b"]"a\n\nb"
    ["x"]"x"
    []""
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free