Write a program that uses a single asynchronous filesystem operation to read a file and print the number of newlines it contains to the console (stdout)
fs.readFile(file, "utf8", (err, buf) => { if (err) { returnconsole.err(err); } const result = buf.toString().split("\n").length - 1; console.log(result); });
Officical Solution:
1 2 3 4 5 6 7 8
var fs = require("fs"); var file = process.argv[2];
fs.readFile(file, function(err, contents) { // fs.readFile(file, 'utf8', callback) can also be used var lines = contents.toString().split("\n").length - 1; console.log(lines); });