Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops What are Loops?

Dirk Mathu
Dirk Mathu
4,050 Points

dumb question (probably)

My generated numbers always display on one long line, even when i put them in a container div on my own created web-page, is this normal for JS? Given that on the video it doesn't seem to be the case, what am i doing wrong?

5 Answers

Hi Dirk,

From what you're describing, it would indicate that you're not concatenating a space between each number.

If the numbers are squished together then the browser treats it as one really long word. By default the browser will not break a long word onto mulitple lines. Instead it will simply overflow its container and you'll get the horizontal scrollbar that you're experiencing.

If the numbers have spaces between them then its treated as multiple words and they will naturally flow to a new line when there's no more space on the current line.

This is what the instructor uses:

document.write(randNum + ' ');

Double check that you have a space between those single quotes. This insures a space will be concatenated after each number.

Dirk Mathu
Dirk Mathu
4,050 Points

Thanks a lot that did the trick, i put document.write(randNum + ''); but there was no actual space between the ''. Silly mistake !

Steven Parker
Steven Parker
229,732 Points

Once you put information on a page, it then is displayed using HTML and CSS rules.

For text to show on different lines in an HTML document, it needs more than a new line in the source. There are many ways to accomplish this, and one of the simplest is just to put "<br>" at the end of each line.

You might want to take some HTML and CSS courses so supplement your JavaScript learning if you're not already in a track that includes them.

Dirk Mathu
Dirk Mathu
4,050 Points

Where do I put a <br>, in a do while loop? And won't it give a strange view with each new number wrapping to a new line?

Steven Parker
Steven Parker
229,732 Points

You forgot to show your code, but assuming you have something like this example from the video:

    console.write(randNum + ' ');

You can add a line break to each number by doing this instead:

    console.write(randNum + '<br>');

Also, note that containing something in a division doesn't automatically change the visual style of the contents, though you can make that happen using CSS.

And, if you have 100,000 numbers, you might not want to display them on individual lines, either. Some of those other courses I mentioned will cover better ways of displaying large amounts of data.

Dirk Mathu
Dirk Mathu
4,050 Points

Thanks for your help i found the fault, i put document.write(randNum + ''); but there was no actual space between the ''. Silly mistake !

Steven Parker
Steven Parker
229,732 Points

Good deal :+1: Remember to always share your code so you can a complete and accurate answer the first time.

Jonathan Dewitt
Jonathan Dewitt
8,101 Points

Even in the video, the teacher's number outputs are all on one line until they reach the end and then wrap down to a second line. You aren't doing anything wrong and this is normal behavior.

Dirk Mathu
Dirk Mathu
4,050 Points

With me the line doesn't end, I have to scroll to the left to see where it ends, and I am talking about 100000 number. Also in the video it seems like the numbers are contained in a division of sorts.

SURENDRA MALLA
SURENDRA MALLA
3,727 Points

document.write(randNum + ", "); may help you see " , " in between the numbers 428, 316, 497, 405, 228, 360, 267, 165, 33, 297,