learnyounode-solutions icon indicating copy to clipboard operation
learnyounode-solutions copied to clipboard

io.js solution bug when using .split()

Open alphalykos opened this issue 10 years ago • 1 comments
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

alphalykos avatar Oct 09 '15 10:10 alphalykos

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.

rvoortman avatar Jan 03 '16 15:01 rvoortman