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
rocky roy
556 Pointsdocument.getElementByID.innerHTML = 'example' ; showing nulll
function RandomNo(upper) { return Math.floor(Math.random() * upper) + 1; } var i = 0; while (i < 100) { var HoldRandomNo = RandomNo(10); document.getElementById('demo').innerHTML = HoldRandomNo + ' '; i += 1; }
3 Answers
jcorum
71,830 Pointsrocky, not sure if this is the issue, as you didn't give a copy of your html, but in order for this line of code to work:
document.getElementById('demo').innerHTML = ...;
there must be an element in the html, and only one element, whose id is demo ( <... id="demo" ...></...>
jcorum
71,830 Pointsrocky, I ran your code in an online JavaScript tester (https://jsfiddle.net/) with the body element you sent (you don't need your html or head as they supply their own), and it ran fine. Each time the result was a different random number.
Here's the html I used:
<body>
<div id="demo"></div>
</body>
And here's the Javascript:
function RandomNo(upper) {
return Math.floor(Math.random() * upper) + 1;
}
var i = 0;
while (i < 100) {
var HoldRandomNo = RandomNo(10);
document.getElementById('demo').innerHTML = HoldRandomNo + ' ';
i += 1;
}
I'm puzzled by your code.
You loop 100 times, but display each of the 100 random numbers on top of the previous one, so that at the end you have only 1 number displayed on the page. And because modern computers run so fast, you don't even see the numbers change.
So why the loop? Just testing loop code? Or?
But anyway, it runs without error. So the null must be from some other problem.
rocky roy
556 Points<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="demo"></div>
<script src="script.js"></script>
</body>
</html>