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

Please, someone help with while statement. Why do I get fifty five.

<script> let total = 0 
let count = 1;
while (count <= 10) {
total += count;
count++;
}
alert(total);
 </script>

1 Answer

Cameron Childres
Cameron Childres
11,818 Points

Hey there~

I just logged each iteration to the console, here's how you're ending up at 55:

 Total of 0 + Count of 1 = 
1
 Total of 1 + Count of 2 = 
3
 Total of 3 + Count of 3 = 
6
 Total of 6 + Count of 4 = 
10
 Total of 10 + Count of 5 = 
15
 Total of 15 + Count of 6 = 
21
 Total of 21 + Count of 7 = 
28
 Total of 28 + Count of 8 = 
36
 Total of 36 + Count of 9 = 
45
 Total of 45 + Count of 10 = 
55

Here's how I logged it:

let total = 0 
let count = 1;
while (count <= 10) {
  console.log(`Total of ${total} + Count of ${count} =`);
  total += count;
  console.log(total);
  count++;
}
alert(total);

Hope this helps!