Master JavaScript
Taking the comments out
Before a program is sent to a browser it is usually shrunk, and the first thing a shrinker throws away is the comments, because the engine ignores them anyway. You are writing the simplest useful version of that step.
Given the text of a file, remove every // comment and hand back what is left.
// onwards, including the slashes themselves.Input:
// set the price
const price = 20; // in pounds
console.log(price);
Line by line:
"// set the price" -> "" dropped
"const price = 20; // ..." -> "const price = 20;" kept, spaces trimmed
"console.log(price);" -> "console.log(price);" kept unchanged
Output:
const price = 20;
console.log(price);source is at most 4000 characters, with lines separated by a single newline. It may be empty, and the answer for an empty input is the empty string.// inside a piece of quoted text, so you never have to tell a comment from a web address. A real shrinker does have to, which is why real ones contain a parser.// comments appear. There are no /* */ blocks.Run your code to see how it does on the examples. Submit also runs the hidden cases.