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 Create a while Loop

why document.write may not be called in js file ? help

help me to make the code run and prints something 26 times on the document.write

script.js
var count = 0;
while (counter < 26){
  var counter= counter (26);
   document.write(counter + ' ');
  counter += 1;
}

3 Answers

Curtis Murley
Curtis Murley
2,766 Points

Line one refers to "count" while line two refers to "counter"

Sanjay Tailor
Sanjay Tailor
8,443 Points

Try this code:

for( var i = 0;  i < 26; i = i + 1 ) {
    document.write("Printing something");
    }
Sanjay Tailor
Sanjay Tailor
8,443 Points

Using while loop, you can do following:

var counter = 0;
while (counter < 26){
   document.write('Print Something ');
   counter += 1;
}

initialize the counter to 0; while loop (26 iteration); print; increase the counter

Hope this helps!

Morten Larsen
Morten Larsen
12,275 Points

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

var counter = 0; while(counter < 100) { var randNum = randomNumber(6); document.write(randNum + " "); counter += 1; }