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

Playing with the begginer JavaScript loops - how do I print all new sets of 8 numbers on a new line?

Im trying to make the random number generator to a lotto number generator code. I put the generator into a function with numbers 1-45 8 times. then I created another function that calls the generator x times, and it is working fine only that all results are printed to 1 long sequence of numbers. How do I add a new line after each set of 8 numbers? Also is there a better way to call the function x times?

function randomNumber(upper) {
  return Math.floor( Math.random() * upper ) + 1;
}
function lotto(){
var counter = 0;
while (counter < 8) {
  var ranNum =  randomNumber(45);
  document.write(ranNum + ' ,' );
  counter += 1;
}
}

var CallLotto = 0;
for(i =0; CallLotto<5; i++) {
  CallLotto += 1;

  lotto();
}

3 Answers

You could add a HTML line break as below:

document.write(ranNum + ',<br/>' );

thnk you but i would like to do ut with javascript

Wouldn't this add a break after every number? I would like to have the in sets of 8

I'm really new at javascript but I gave it a shot and this is what I came up with.

var html = '';
var lotoNumbers;

function randomNumber(upper) {
  return Math.floor( Math.random() * upper ) + 1;
}

function number () {
  var nums = '(';
  nums += randomNumber(45) + ', ';
  nums += randomNumber(45) + ', ';
  nums += randomNumber(45) + ', ';
  nums += randomNumber(45) + ', ';
  nums += randomNumber(45) + ', ';
  nums += randomNumber(45) + ', ';
  nums += randomNumber(45) + ', ';
  nums += randomNumber(45) + ')';
  return nums;
}

function print(message) {
  document.write(message);
}

for (var i = 0; i < 8; i += 1) {
  var lotoNumbers = number();
  html += 'Your lucky loto numbers are ' + lotoNumbers + '<br/>';
}

print(html);

Good idea , so I have the first part working but instead of just calling the function 5 times after each function call load the results in a variable..I'll try when I get home...thank you