javascript - code breaks console.log()! -


i don't understand why i'm getting output:

var frenchwords; fs = require('fs')  var data = fs.readfilesync('frenchverbslist.txt', 'utf8'); frenchwords = data.split('\n'); console.log(array.isarray(frenchwords)); //true   var x = frenchwords[0]; console.log("a: " + "look: " + x + typeof(x)); //outputs "stringk: abaisser" 

i don't understand why output isn't "a: look: abaisserstring"

any explanation of what's going on gratefully received :-)

gerard

it's happening because file's lines of text terminated \r\n, not \n can reproduce with:

var x = 'abaisser\r'; console.log("a: " + "look: " + x + typeof (x)); 

this outputs "stringk: abaisser" because cr (\r) char returns output cursor beginning of line string overrwrites output a: loo chars.

so try changing data.split call to:

frenchwords = data.split('\r\n'); 

or ismael suggested in comments, use regex matches of common line terminations of cr, lf, or crlf using:

frenchwords = data.split(/\r?\n|\r/g) 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -