Master JavaScript
A linter for lines that will be swallowed
Your team has decided to write JavaScript without semicolons. That is a workable choice, and it comes with one obligation: any line that begins with certain characters has to be given a leading semicolon, or the parser will join it to the line above.
You are writing the check that runs before every commit. Given the text of a file, count how many lines are exposed.
Split the text into lines. A line is exposed when all three of these are true.
(, [, a backtick, + or -.const sum = a + b
(a + b).toString()
console.log(sum)
line 1 const sum = a + b first line, skipped
line 2 (a + b).toString() starts with ( counts
line 3 console.log(sum) starts with c no
Output: 1Line 2 is the one that matters. Without a semicolon at the end of line 1, the parser reads a + b(a + b) and the program fails at run time with a message about calling something that is not a function.
Input: "" Output: 0
Input: "(1).toString()" Output: 0 the only line is the first
Input: "let a = 1\n\n [0]" Output: 1 blank line skipped, spaces ignoredsource is at most 4000 characters, with lines separated by a single newline. It may be empty./ on purpose, because telling a comment from a division needs a real parser.Run your code to see how it does on the examples. Submit also runs the hidden cases.