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 A Closer Look at Loop Conditions

Code not working!

In the challenge section the question was create a while loop that runs 26 times, dont forget to place document.write inside it.

My code was:

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

I didnt pass the test. Its saying document.write method was not called.

document.write method was not called-WHAT DOES IT MEAN?

5 Answers

javascript

var count = 0;

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

The challenge indicates to create a loop in order to print document.write 26 times. The first line of code indicates that the count starts with 0 which it should. Then, you see inside the while function? Inside we have declared document.write() function as it asks in the challenge. As you can see, there is not specifics on what to print out inside the document so I chose the string "hello" for the example.

Steven Parker
Steven Parker
231,269 Points

:point_right: The error message may be misleading.

I looks to me like your document.write method would be called a lot, over and over forever.

Where you have "while(count=26)", the value of count will be set to 26, and since this is "truthy", the loop will continue without end.

I suspect you may have intended to write "while(count<26)", to check if the count is still less than 26. That would produce the correct result.

Thanks Karolin and Steven.

@ yes I tried "while(count<26)" but I didnt pass the test. It showed document.write was not called.

However this morning the same code worked I mean count<26.

Best regards

I did not see that you got it figured out before I replied lol

You have a logical error, in your condition you put the assigment operator (=), for this is better use comparasing (<) because you need to compare in each loop the value of the count variable with the limit condition tha you spesificated. i dont recomended use the equality(==) thats mean that your loop only run when the count value has the same value you indicate to the condition.

Karolin Rafalski
Karolin Rafalski
11,368 Points

Becuase you declare 'count = 0;' It will never enter the while loop, becuase the condition of the loop is 'while count = 26'

Steven Parker
Steven Parker
231,269 Points

That's not the problem. Setting the count to 0 to start with is a good thing.

I think you're confusing the assignment operator (=) with an equality test (==).