learnyounode-solutions
learnyounode-solutions copied to clipboard
io.js solution bug when using .split()
trafficstars
"the test file does not have a newline character ('\n') at the end of the last line"
Test file like,
foo\n bar\n foo2\n bar2
should give only 3 newline characters.
On using .split() the array obtained is
[ 'foo','bar','foo2','bar2','']
and
.split().length - 1
gives 4
it should be
.split().length - 2
you are sure you don't have a trailing newline?
I use the following:
data = data.split("\n");
console.log("Number of newlines: " + (data.length - 1));
and it always returns just what i expect. Your array suggests that there IS a newline after 'bar2', because there is an empty string in it. Double check your testfile.