For loop problem i am quite not clear about the loop for power exponent

i am not quite clear about the loop line 3 and final result i know it’s print 2 power 10 which is 1024

Could you clarify your question? What makes you confused, did you expect different result?

1 Like

i do not quite understand too what do you really want it to do

1 Like

First of all, please cut and paste code - don’t take pictures. It’s easier to help you.

Your question:

i am not quite clear about the loop line 3…

I think you are asking what:

  result = result * 2;

does.

First you need to understand that the = sign in JS is not an “equation” in the traditional sense of the word. It is an assignment. (I wish they used a different symbol - in some languages they do.) It is saying “take what is in the right side of me and put it into the variable on the left side of me”. It is saying, “take ‘result * 2’, evaluate it, then store it into ‘result’”. Does that makes sense? So in this particular case, it is saying to double whatever is in result and double it.

On the first pass, result is 1. So:

  result = result * 2;
  // becomes ...
  result = 1 * 2;
  // becomes ...
  result = 2;

On the next pass, result is 2. So:

  result = result * 2;
  // becomes ...
  result = 2 * 2;
  // becomes ...
  result = 4;

On the next pass, result is 4. So:

  result = result * 2;
  // becomes ...
  result = 4 * 2;
  // becomes ...
  result = 8;

Because of the for loop, it does this 10 times.

Does this make sense?

2 Likes

Thanks a lot , that make sense . now i do understand .

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.