views:

190

answers:

3

I have this file

var paragraph = "Abandon| give up or over| yield| surrender| leave| cede| let go| deliver| turn over| relinquish|  depart from| leave| desert| quit| go away from| desert| forsake| jilt| walk out on |  give up| renounce| discontinue| forgo| drop| desist| abstain from|  


recklessness| intemperance| wantonness| lack of restraint| unrestraint|  


abandoned |left alone| forlorn| forsaken| deserted| neglected| rejected| shunned| cast off | jilted| dropped| ";

with a lot of spacing, so it's giving me that error at the spacings.

then running a loop and alerting the output

var sentences = paragraph.split("|");
var newparagraph = "";

for (i = 0; i < sentences.length; i++) {
    var words = sentences[i].split(" ");
    if (words.length < 4) {
        newparagraph += sentences[i] + "|";
    }
}
alert(newparagraph);

how do I read from a file that doesn't get errors from spacing?

A: 

I can't see a quote character at the end of var paragraph = "... line.

Mehrdad Afshari
A: 

You're missing a terminating quote and semi-colon at the end of the var paragraph assignment. You can use a tool like jslint (http://www.jslint.com/) to check your syntax, if in doubt.

Noah
+3  A: 

As the other answers noted, javascript automatically puts a semicolon at the end of (or what it thinks is) every statement. More about it here. http://en.wikipedia.org/wiki/JavaScript_syntax#Whitespace_and_semicolons

It doesn't understand text with line-breaks in them. You could use the '\' character to signify your text contains line-breaks.

var text = "this is\
   a very long\
   sentence";

But the above practice is generally frowned upon. Best bet, define strings in one line or use concatenation (+) to break your strings into multiple lines. If your text must contain line-breaks, use '\n' character.

Chetan Sastry