Start learning free

LESSON 9 OF 70 · PROJECTS: COMMAND-LINE TOOLS

wc in TypeScript: Count Lines with length

Practice Read the full explanation

Build a tiny version of the Unix wc command. Given a list of lines, return the total number of lines as an integer.

Function signature:

countLines(lines: string[]): number

Example:

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

Output: 5

Every entry of the list is one line, even an empty string "". An empty list has 0 lines.


The number of lines is just the length of the input array. Use the array's length property — no loop needed.

A one-liner:
class Solution {
    countLines(lines: string[]): number {
        return lines.length;
    }
}

STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: COMMAND-LINE TOOLS

    Wc V1 in TypeScript, explained

    Problem overview

    The Unix wc command counts things in text. This first version counts lines. The method is countLines(lines: string[]): number, and it returns how many lines the list holds.

    For ["hello", "world", "foo", "bar", "baz"] the result is 5. Every entry is one line, even an empty string "", and an empty list has 0 lines.

    Approach: the array already knows its size

    The input is an array in which each element is a line. The number of lines is the number of elements, and JavaScript arrays keep that number in their length property. No loop and no counter are needed.

    A loop that adds 1 for each element would give the same answer, but it repeats work the array already did. The one thing to be careful about is what counts as a line: here every element does, including blank ones. A list such as ["a", "", "b"] has 3 lines. Nothing is skipped or trimmed.

    The counting loop looks like this, and it is what length replaces:

    let count = 0;
    for (const line of lines) {
        count++;
    }
    

    For the five-line example both give 5. The loop does more work for the same answer, and it adds a variable that has to be started at 0 and kept correct.

    Step by step

    1. Return lines.length. The property holds the number of entries, so it is the answer as it stands.

    The solution in TypeScript

    class Solution {
        countLines(lines: string[]): number {
            return lines.length;
        }
    }
    
    • length is a property, so it is written without parentheses. With lines.length() the compiler reports This expression is not callable. Type 'Number' has no call signatures. If the compiled JavaScript runs anyway, it stops with TypeError: lines.length is not a function.
    • The return type number matches: length is always a whole number that is zero or more.
    • Strings have a length too, but it counts characters. Here the argument is an array of strings, so lines.length counts the strings and lines[0].length would count the characters of the first one.

    This differs from the real wc -l, which counts newline characters in a file. Here the input is already split into lines, so the count is of entries. The two agree when every line, including the last, ends with a newline.

    Calling it is a one-liner: new Solution().countLines(["hello", "world", "foo", "bar", "baz"]) returns 5.

    The property is always one more than the index of the last entry. The first line is lines[0] and the last is lines[lines.length - 1], so a five-line list has indexes 0 to 4. The value stays current as the array changes: after push adds an entry, length is one larger, with nothing to update by hand.

    Complexity

    Reading length takes constant time and uses no extra space, however many lines there are.

    Edge cases and common mistakes

    • Empty list. The result is 0.
    • Blank lines. They count. ["", ""] has 2 lines, and [""] has 1 line, not 0, even though it holds no text.
    • Counting characters or words. This version counts lines only. Using lines.join("").length would count characters instead.
    • Calling length like a method. Write lines.length, not lines.length().

    Testing

    InputResult
    ["hello", "world", "foo", "bar", "baz"]5
    ["a", "", "b"]3
    []0
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free