Search code examples
javascriptrepeat

javascript repeat() display each result in a new line.


Hi I am working with js and when creating an automatic list I use var str = "<li>...content...</li>"; str.repeat(prompt('How Many Items'))

and it displays like this

<li>...content...</li><li>...content...</li><li>...content...</li><li>...content...</li><li>...content...</li>

and I would like to display it like this:

<li>...content...</li>
<li>...content...</li>
<li>...content...</li>
<li>...content...</li>
<li>...content...</li>

this is strictly in code view as it is a line of code that it is being generated once the number of times to repeat has been defined.


Solution

  • Add a line break at the end.

    Change this:

    var str = "<li>...content...</li>";
    

    To this:

    var str = "<li>...content...</li>\n";