How do I create a new line in Javascript?

2019-01-05 00:05发布

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.

15条回答
我想做一个坏孩纸
2楼-- · 2019-01-05 01:06

Use "\n":

document.write("\n");

Note, it has to be surrounded in double quotes for it to be interpreted as a newline. No it doesn't.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-05 01:06

your solution is

var i;
for(i=10; i>=0; i= i-1){
   var s;
   for(s=0; s<i; s = s+1){
    document.write("*");
   }
   //printing new line
   document.write("<br>");
}
查看更多
Lonely孤独者°
4楼-- · 2019-01-05 01:07
document.write("\n");

won't work if you're executing it (document.write();) multiple times.

I'll suggest you should go for:

document.write("<br>");

P.S I know people have stated this answer above but didn't find the difference anywhere so :)

查看更多
登录 后发表回答