Take the 2-minute tour ×

Program that calculates and shows the value of (2 to the 10th power)

This statement results in 1024. I'm not understanding how it keeps looping after it reaches "9". Does "< 10" mean loop around ten times, or loop up to a sum less than "10"? Would appreciate someone explaining this to me. Thanks!

var result = 1;
var counter = 0;
while (counter < 10) {
  result = result * 2;
  counter = counter + 1;
share|improve this question
unitl counter value is less than 10 –  Fabrizio Calderan Jun 22 '12 at 15:41
A simple alert should have been enough for you to realize what was going on. Debugging, it helps. –  TheZ Jun 22 '12 at 15:42
Consider adding a log statement or using the debugger when you're not sure of behavior. –  Dave Newton Jun 22 '12 at 15:42
Didn't add alert or console.log because I thought you guys would write whatever you wanted to run the code. –  Chris22 Jun 22 '12 at 15:46
That isn't the attitude to have: You should always try to work things out yourself first before just jumping on here to ask... Console.log(counter), or alert(counter) would have helped you work this out. –  phenomnomnominal Jun 22 '12 at 15:50
show 2 more comments

6 Answers

up vote 10 down vote accepted

Your counter is running ten times, once each for the values 0-9. When writing loops like that (that include a <) I think of the 10 as "this loop will be running ten times." It's helped a lot with minor issues like this.

Remember, there are only two really hard things in programming: cache validation, variable substitution, and off-by-one errors.

share|improve this answer
All four of those problems always get me. –  TheZ Jun 22 '12 at 15:54
add comment

When counter is 1 it result is 2^1 When counter is 2 it result is 2^2

Since counter is 10 at the end of the loop, result is 2^10.

share|improve this answer
add comment
loop 1: result = 1*2 (2) counter = 1
loop 2: result = 2*2 (4) counter = 2
loop 3: result = 4*2 (8) counter = 3
loop 10: result = 512*2 (1024)counter = 10
share|improve this answer
Thanks, Kevin. I'm still learning javascript and it was great of you to dumb it down for me. Appreciate it! –  Chris22 Jun 22 '12 at 15:57
add comment

The program will only step inside the loop if the condition for the while-loop is met. So you check it before entering the loop.

If you want the program to step inside the loop one more time, either use a do-while loop where you check the condition after the execution of the loop. You can also change the condition to "counter <= 10" and use the while-loop as is.

Here you can find more information on while-/do-while-loops and also breaks in javascript.

share|improve this answer
add comment

Yes ten times 0 to 9 = 10.

When the compiler sees if 10 < 10 it evaluates to false. If you want to see 10 change the condition to <=

share|improve this answer
add comment

It loops 10 times and as such it multiplies by two ten times and as such gives you 2^10 = 1024. Exactly. P.S. If you only want this multiplication, you'd be better off with

result = 1 << 10
share|improve this answer
Thanks, Panda. I'm going through an online tutorial and I needed more understanding on the solution code. –  Chris22 Jun 22 '12 at 15:58
add comment

Your Answer


