var i;
for(i=10; i>=0; i= i-1){
var s;
for(s=0; s<i; s = s+1){
document.write("*");
}
//i want this to print a new line
/document.write(?);
}
I am printing a pyramid of stars, I can't get the new line to print.
var i;
for(i=10; i>=0; i= i-1){
var s;
for(s=0; s<i; s = s+1){
document.write("*");
}
//i want this to print a new line
/document.write(?);
}
I am printing a pyramid of stars, I can't get the new line to print.
Use the
\n
for a newline character.You can also have more than one:
However, if this is rendering to HTML, you will want to use the HTML tag for a newline:
The string
Hello\n\nTest
in your source will look like this:The string
Hello<br><br>Test
will look like this in HTML source:The HTML one will render as line breaks for the person viewing the page, the
\n
just drops the text to the next line in the source (if it's on an HTML page).For a string I just write
"\n"
to give me a new line. For example, typingconsole.log("First Name: Rex" + "\n" + "Last Name: Blythe");
Will type:First Name: Rex
Last Name: Blythe
Use a
<br>
tag to create a line break in the documentHere's a sample fiddle
\n dosen't work. Use html tags
If you are using a JavaScript file (.js) then use
document.write("\n");
. If you are in a html file (.html or . htm) then usedocument.write("<br/>");
.Alternatively, write to an element with the CSS
white-space: pre
and use\n
for newline character.