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

Rasbin Rijal
PLUS
Rasbin Rijal
Courses Plus Student 10,864 Points

Javascript while loop challenge task 1 confusion

Challenge Task 1 of 1

Q) 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.

My code goes here :

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

The code prints 26 lines of Hello and passes the challenge. In my code inside the loop, I have first incremented the counter by 1. Then below it is actual printing of Hello done. So, shouldn't the first value of counter be 1 inside the loop and print only 25 times since my while loop counts less than 26. But, I am not clear why is the first value of count 0 and not 1.

Can anybody explain it? Thanks

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

3 Answers

Steven Parker
Steven Parker
231,269 Points

It doesn't matter to the number of loops if you increment the count before the write or after the write.

:point_right: What matters is that the count is tested (in the while) before it is incremented.

Your second piece of code is the perfect proof of what's happening. Even though your loop runs while count < 26, the write statement prints out numbers from 1 to 26. That's 26 numbers (including 26!).

Rasbin Rijal
Rasbin Rijal
Courses Plus Student 10,864 Points

Thank you Steven Parker . Can you provide the link to the resource where I can find information about how to use those cool pointers like you have written on the second line in your answer?

Steven Parker
Steven Parker
231,269 Points

:white_check_mark: Sure thing. I have way too much fun with them myself :exclamation:

:point_right:They can be found on the Emoji Cheat Sheet :point_left:

:warning: Not all of them work here, but the vast majority do.

Rasbin Rijal
Rasbin Rijal
Courses Plus Student 10,864 Points

Hi Nicolas, thank you for reference. My real question was about how the first value of count becomes 0 and prints the first line when I increment in the first step as the value of count as 1. I figured out that even though I incremented value of count by 1, the printing is done when count = 0.

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