Start learning free

LESSON 2 OF 70 · PROJECTS: COMMAND-LINE TOOLS

cat -n in TypeScript: Number Lines with padStart

Practice Read the full explanation

Now build the -n flag of the cat tool, as a new function. It prints every line preceded by its 1-based line number, right-aligned in 6 columns, then a tab character, then the line text. Blank lines are numbered too, and an empty list gives an empty string.

Function signature:

numberedCat(lines: string[]): string

Format per line: the line number right-aligned in a field 6 characters wide (padded with spaces on the left), then a literal tab (\t), then the original line.

Example:

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

Output:
"     1\thello\n     2\tworld\n     3\tfoo"

When printed:
     1	hello
     2	world
     3	foo

Loop with an index, build each formatted line, then join them with "\n". String.prototype.padStart handles the right-alignment in 6 columns.

For each index i, build the line as String(i + 1).padStart(6, " ") + "\t" + lines[i], push it to an array, then return out.join("\n").

The full solution:
class Solution {
    numberedCat(lines: string[]): string {
        let out: string[] = [];
        for (let i = 0; i < lines.length; i++) {
            out.push(String(i + 1).padStart(6, " ") + "\t" + lines[i]);
        }
        return out.join("\n");
    }
}

STEP 1 OF 4
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: COMMAND-LINE TOOLS

    Cat V2 in TypeScript, explained

    Problem overview

    Version 2 of the cat project adds the -n flag as a new function, numberedCat(lines: string[]): string. Every line is printed with its 1-based line number, right-aligned in a field 6 characters wide, then a tab character, then the line itself. Blank lines get numbers too, and an empty list gives an empty string.

    For ["hello", "world", "foo"] the result is " 1\thello\n 2\tworld\n 3\tfoo". Printed, it looks like this:

         1	hello
         2	world
         3	foo

    The previous version joined the lines with newlines. This one changes each line first and then joins.

    Approach: build each row, then join

    Each output row depends on two things: the line and its position. A for loop with an index i gives both: lines[i] is the line, and i + 1 is its number, because the index starts at 0 and line numbers start at 1.

    The number has to be exactly 6 characters wide. A number is not text yet, and padStart belongs to strings. So convert it with String(i + 1), then call padStart(6, " "), which adds spaces on the left until the text is 6 characters long. A number that is already 6 characters or longer is left as it is.

    The rows go into an array, and one join("\n") at the end puts a newline between each two, with none after the last row.

    Step by step

    1. Create an empty array out of type string[] for the finished rows.
    2. Loop over the positions with i from 0 while i < lines.length. You need the index twice: for the number and to pick the line.
    3. Push one row per line: the padded number, a tab, and lines[i]. The number is i + 1, not i.
    4. After the loop, return out.join("\n"). The newline goes between rows only.

    The solution in TypeScript

    class Solution {
        numberedCat(lines: string[]): string {
            let out: string[] = [];
            for (let i = 0; i < lines.length; i++) {
                out.push(String(i + 1).padStart(6, " ") + "\t" + lines[i]);
            }
            return out.join("\n");
        }
    }
    
    • padStart(6, " ") pads at the start, so numbers line up on their right edge. Its sibling padEnd would pad after the text and left-align the numbers.
    • The padding is applied to the number only. The tab and the line are added afterwards, so the line's own text is never padded or cut.
    • "\t" is one tab character. Terminals show it as a jump to the next tab stop, so the text after it may look aligned differently from the spaces in a web page.
    • String(i + 1) is needed because padStart exists on strings, not numbers. Calling it on a number is a type error that the compiler reports.

    Complexity

    The loop runs once per line, and each row takes time proportional to its length, so the total is O(total length) for both time and space.

    Edge cases and common mistakes

    • Off by one. Writing String(i) numbers the lines from 0. The first row must say 1.
    • Blank lines. They get a number and a tab like any other: ["", "x"] gives " 1\t\n 2\tx".
    • Empty list. The loop doesn't run, out stays empty, and join returns "".
    • Numbers wider than 6. They aren't cut: line 1000 prints as " 1000", and a seven-digit number would simply be wider than the field.
    • A trailing newline. Adding "\n" inside the loop would leave one after the last row. Joining at the end avoids it.

    Testing

    InputResult
    ["hello", "world", "foo"]" 1\thello\n 2\tworld\n 3\tfoo"
    ["", "x"]" 1\t\n 2\tx"
    []""
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free