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

Agnes Caringal
Agnes Caringal
6,239 Points

While loop codes... what's wrong with my codes

did i forgot something? thanks

script.js
var count = 0;

while (count > 26) {
  document.write("<p>Loop Numbers # "+ count + </p>");
  count += 1;
}

6 Answers

Agnes Caringal
Agnes Caringal
6,239 Points

guys i already solved...it goes like this

var count = 0;

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

This code will not run since count is equal to 0 and the while loop will only run if count is greater than 26.

Your really close on this. There are a couple of things you need to change.

  1. change the greater than sign to equal that count is less than "<" 26 this way the loop with exit at 26, because its asking for the loop to run up to 26 times,

  2. in the document.write() the only parameter needed is the count var. nothing else.

so document.write(count);

everything else looks good though. I hope this helps.

var count = 0;
while (count < 27) {
document.write(count);
count =+1;      
}

I keep getting the error message " Bummer! Your code took too long to run."

Is there something wrong with the code?

BTW, shouldn't it be

while (count < 27) {

as the loop needs to run 26 times, which is < 27?

Just realised I put

count =+ 1; 

instead of

count += 1; 

live and learn!

Agnes Caringal
Agnes Caringal
6,239 Points

solved already..check my previous comment...thanks Jacob Mishkin :)

I did, and awesome job!! I voted your answer as the best answer, there is nothing better than solving your own problems. Happy coding.

Not a problem.