0

I would like my program to return 10 random numbers but it isn't working it doesn't load the random numbers, I have linked the program to index.html so that's not the problem here, this is what I have tried:

function randomNumber(upper) {
    return Math.floor(Math.random() * upper) + 1;
}
var counter = 0;
while (counter < 10) {
    var randNum = randomNumber(6);
    document.write(randNum + ' ');
    counter += 1;
}
5
  • So what does "it isn't working" mean? Are you waiting for onload? Commented Nov 12, 2015 at 12:38
  • meaning it doesnt load the random numbers Commented Nov 12, 2015 at 12:39
  • maybe you are printing the results in the header of the page ... Commented Nov 12, 2015 at 12:40
  • ill add the index. html and style sheet too Commented Nov 12, 2015 at 12:44
  • i added it to js fiddle and it worked but for some reason it doesn't work on teamTreehouse (its the site im learning javascript from) Commented Nov 12, 2015 at 12:46

1 Answer 1

1

If it works in fiddle but not elsewhere it's most probably a problem with onload here as Doorknob already suggested.

function randomNumber(upper) {
    return Math.floor( Math.random() * upper ) + 1;
}
window.onload = function () {
  var counter = 0;
  while (counter < 10) {
    var randNum = randomNumber(6);
    document.write(randNum + ' ');
    counter += 1;
  }
};

The bracketing with window.onload causes the code to wait until the whole page is loaded.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.