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

Aaron HARPT
Aaron HARPT
19,845 Points

code challenge problem

I am having trouble with a while loop on this code challenge. It tells me that my code takes too long to run.

In this challenge, you will create a while loop that prints to the document 26 times. We've added a variable named count, use it to track the number of times the loop runs. Don't forget to use the document.write() method inside the loop.

script.js
var count = 0;
while (count < 26) {
  document.write("count");
}

9 Answers

hey, you are printing out a string "count" and not your variable, the program will write anything you write inside parentheses as it is , and plus, you re not incrementing your variable count, so its obvious that it is gonna be an endless loop, so inside your while loop you have to increment your variable, so you ay wanna try this :

var count = 0;
while (count < 26) {
  document.write(count);
count += 1;  // or you can write it like that : count++. its the same as count = count + 1;
}
Victor Learned
Victor Learned
6,976 Points

You code takes too long to run because you have created an infinite loop. You need to increment count within your while loop to ensure it will eventually stop running.

var count = 0

while(count < 26)
{
   document.write(count); 
   count++;
}
Ahkeem Lang
Ahkeem Lang
16,358 Points

Why does count ++; run faster than count += 1; ??

On this particular question. Guil wants us to output to the console. So the correct answer is:

let count = 0; 
while (count < 26) {
  console.log(count);
count += 1;  
}
  1. We first declare the variable count with | let count = 0; |
  2. We then open the while loop with | while () |
  3. We declare that we only want the loop to run 26 times by inserting | count < 26
  4. Then we log that loop to the console with | console.log(count)
  5. Then we increment the count variable by 1 each time with | count +=1

You have to do update your counter at the end of your loop, or else your loop will keep on running forever.

Justice Omorodion
PLUS
Justice Omorodion
Courses Plus Student 8,315 Points

The code can also run like this:

var count = 0

while (count < 26) { document.write(26) count += 1 }

Sandeep Krishnan
Sandeep Krishnan
9,730 Points

what id you say document.write( "count"). What I meant it would it even matter it document.write returns a count variable or string ?

let count = 0 while (count <26) { console.log(count) count +=1 }

this works

let count = 0; while(count < 26){ console.log(count); count +=1; }