I can't speak for whatever you've actually done until I see it. I can tell why the first of these is unterminated:
Code:
var question = new Array('Q. 1. <br />
A. ', 'Q. 2. <br /> A.', 'Q. 3. <br /> A.');
Look at the line break. The javascript parser can assume a semi-colon there, so you would in effect have:
Code:
var question = new Array('Q. 1. <br />;
A. ', 'Q. 2. <br /> A.', 'Q. 3. <br /> A.');
That means that the first line must stand on its own. Looking at just it:
Code:
var question = new Array('Q. 1. <br />;
we see an opening delimiter for the string, but no closing one. The type of string is a literal (as opposed to a string Object created with the new String() method). It's not terminated. It's an unterminated string literal.
Now, with this one:
Code:
var question = new Array('Q. 1. <br />' +
'A. ', 'Q. 2. <br /> A.', 'Q. 3. <br /> A.');
the string on the first line is terminated. Next comes the plus operator, which tells the parser we are going to add something to the string. This also allows the parser to know that this line isn't really finished yet. What we are going to add to it can be on this line or on the next line. On the next line we have the completion of the string (its second part - another string literal in this case) and then a comma denoting that another array entry is to follow, another string literal, etc.
The preferred method in a case like this though is to split the line on the comma.
Code:
var question = new Array('Q. 1. <br /> A. ',
'Q. 2. <br /> A.', 'Q. 3. <br /> A.');
Here our string is terminated on the first line. Then comes a comma that tells the parser that we will be continuing the array. Once again, the line isn't finished. Like the plus operator for a terminated string, the comma (when used like this in an unterminated array) tells the parser that the line will continue on this line or on the next line with the next array entry. On the next line we have another array entry (a string literal), and so on.
Bookmarks