-1

I looked around and can't figure out why this code isn't working. It's simply supposed to make a checkerboard but I'm getting a single line of symbols:

Supposed to do: # # # #

# # #

# # # #

Is doing:

# # # # # # # etc...

Here is the code

symbol1 = " ";
symbol2 = "#";
for (i=0;i<9;i++){
for (j=0;j<9;j++){
    if (j%2 == 0){
        document.write(symbol2);
}   
    else
        document.write(symbol1);
    document.write("\n");   
}


}
David
  • 43
  • 1
  • 5
  • 1
    Is this in a browser? If so you need to be writing HTML. A line break in HTML is `
    ` not `\n`
    – dmeglio Sep 03 '15 at 15:43
  • You should avoid using `document.write`. – Etheryte Sep 03 '15 at 15:46
  • 1
    @Nit that's a blanket statement and isn't always true. In general you are correct, but there are reasons to use it. – dmeglio Sep 03 '15 at 15:48
  • @dman2306 http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – Etheryte Sep 03 '15 at 15:49
  • @Nit yup, and that's not a universal "never use it." Also the "doesn't work in XHTML" note is moot now as XHTML is effectively dead. It raises good points, but none of them are "never use it for any reason." Just good reasons to be mindful of what you're doing. – dmeglio Sep 03 '15 at 16:23
  • @dman2306 I never said _"never use it"_, you're looking for a useless argument by building a strawman. – Etheryte Sep 03 '15 at 16:27

2 Answers2

0

Since you're outputting to an HTML document, \n doesn't mean newline, it's just whitespace. In HTML, all whitespace is just a single space in the rendered output.

To output a line break, you ouput a br element:

document.write("<br>");

Note that while document.write is fine for small experiments and such, it has very little place in actual web development. Using document.write adds to the document where the script tag is during the main parsing of the document, but once the main parsing is done, the document is closed. If you use document.write again (for instance, in response to an event), it will implicitly do a document.open, wiping out all of the document contents, and then write to the newly-opened document.

But again, it's fine if you're just running some code for experiments and quick learning, just beware of what it actually does. As you progress, you'll start using the DOM instead (either directly, or through a DOM library like jQuery).

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
0

you are writing new line to document which won't work. you need to write breakline tag to be rendered as html. something like this:

symbol1 = " ";
symbol2 = "#";
for (i=0;i<9;i++){
for (j=0;j<9;j++){
if (j%2 == 0){

    document.write(symbol2);
}   
else
    document.write(symbol1);

}
document.write("<br/>");

}
maynaren
  • 26
  • 2
  • Didn't know that about newline. Yeah, I'm only using document.write for learning single. I wouldn't use it on a website (I found out the hard way not to use it). Thanks for the help, it works very well. – David Sep 03 '15 at 15:56