Start learning free

LESSON 601 OF 671 · PROJECTS: COMMAND-LINE TOOLS

cat v3 in JavaScript: Show Tabs and Line Ends

Practice Read the full explanation

Now build the -A flag of the cat tool, as a new function. It makes whitespace visible: every tab character (\t) is replaced by the two characters ^I and every line ends with a literal $ marker (so trailing spaces become obvious). Lines are then joined with newlines.

Note: real cat -A also shows other control characters (such as ^M) and non-ASCII bytes; this version only handles tabs and line ends, like cat -T -E. A blank line becomes just $, and an empty list gives an empty string.

Function signature:

showAllCat(lines)

Example:

Input: lines = ["hello\tworld", "foo"]

Output: "hello^Iworld$\nfoo$"

When printed:
hello^Iworld$
foo$

Another example (a blank line, trailing spaces):

Input: lines = ["a\t", "", "x  "]

Output: "a^I$\n$\nx  $"

When printed:
a^I$
$
x  $

Two transformations per line: replace every tab with "^I", then append "$". After both, join all the transformed lines with "\n".

A simple way to replace ALL tabs is line.split("\t").join("^I"). Then concatenate "$":
lines[i].split("\t").join("^I") + "$"


The full solution:
class Solution {
    showAllCat(lines) {
        let out = [];
        for (let i = 0; i < lines.length; i++) {
            out.push(lines[i].split("\t").join("^I") + "$");
        }
        return out.join("\n");
    }
}

STEP 1 OF 4
    Ctrl + Enter to check

    JAVASCRIPT · PROJECTS: COMMAND-LINE TOOLS

    Cat V3 in JavaScript, explained

    Problem overview

    This version adds a flag that makes whitespace visible, as a new method, showAllCat(lines). Every tab character is replaced by the two characters ^I, and every line gets a literal $ at its end, so trailing spaces become easy to see. The lines are then joined with newlines.

    The real cat -A also shows other control characters, but this version handles only tabs and line ends. A blank line becomes just $, and an empty array gives an empty string.

    For ["a\t", "", "x "] the result is "a^I$\n$\nx $". Printed:

    a^I$
    $
    x  $

    Approach: two changes to each line

    Each line goes through two transformations. First every tab becomes ^I. Then $ is appended. After that the transformed lines are joined with "\n", as in the earlier versions.

    The first change must replace every tab, not only the first. The lesson does it with a pair of calls: split("\t") cuts the string at each tab into pieces, and join("^I") glues the pieces back with ^I between them. A line with no tab splits into a single piece, so it comes back unchanged.

    To see how the split and join pair behaves, look at the pieces: "tab\there".split("\t") gives ["tab", "here"], and "none".split("\t") gives ["none"], one piece, so joining it back changes nothing. A line that is only tabs, like "\t\t", splits into three empty pieces, and joining them with ^I gives ^I^I.

    The marker has a purpose. A line such as "x " looks the same as "x" when printed, because the spaces are invisible. With $ at the end the two print as x $ and x$, and the difference is clear. The same goes for tabs, which can look like several spaces.

    Step by step

    1. Create an empty array named out for the changed lines.
    2. Loop over every line by position, with lines[i] as the current line.
    3. Push the changed line. Split at tabs, join with ^I, then add "$". The marker goes last, after the tabs are handled.
    4. Return the rows joined with "\n".

    The solution in JavaScript

    class Solution {
        showAllCat(lines) {
            let out = [];
            for (let i = 0; i < lines.length; i++) {
                out.push(lines[i].split("\t").join("^I") + "$");
            }
            return out.join("\n");
        }
    }

    Language details

    • "\t" is one tab character. In the output, it appears as the two characters ^I.
    • replace with a string pattern changes only the first match: "a\tb\tc".replace("\t", "^I") gives "a^Ib\tc". The split and join pair changes all of them, and so does replaceAll("\t", "^I"), which gives "a^Ib^Ic".
    • Strings are immutable, so none of these calls changes the original line. Each one returns a new string.

    Testing

    The method returns these results:

    ["hello\tworld","foo"] -> "hello^Iworld$\nfoo$"
    ["\t\t"] -> "^I^I$"
    ["a\tb\tc"] -> "a^Ib^Ic$"
    [] -> ""

    Edge cases and common mistakes

    • Replacing only the first tab. This is the usual trap with replace and a string pattern.
    • Skipping blank lines. A blank line still gets its marker and becomes $.
    • Trailing spaces. They are kept, and the $ after them makes them visible: "x " becomes "x $".
    • Adding the marker to the joined text. The $ belongs to each line, not to the whole string. Appending it once after the join would mark only the last line.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free