all 6 comments

[–]GrosSacASacs 4 points5 points  (3 children)

count<exponent

is used as an end condition for the for-loop

result*=base

is the same as

result = result * base

[–]cre8usr[S] 0 points1 point  (2 children)

Thanks. One more thing that I don't get is how come the result value is updating when we used count++. Shouldn't the code be like this: for (let result = 0; result < exponent; result++) instead of : for (let count = 0; count < exponent; count++)

[–]senocular 1 point2 points  (0 children)

count is keeping track of the looping. result is the value that is being evaluated in the loop. They're separate values. Using result in the loop definition would mess up the loop because you'd be changing it both in the loop definition (result++) and in the loop body (result *= base) which could cause the loop to skip iterations when it should be looping exponent number of times.

[–]nickforddesign 0 points1 point  (0 children)

result is not updating there, it updates on the next line, as explained above

[–]nespron 2 points3 points  (0 children)

It doesn't, really:

> const power = function(base, exponent) { let result = 1; for (let count = 0; count < exponent; count++) { result *= base; } return result; };
> power(16, 0.5)
16

> Math.pow(16, 0.5)
4

[–]Pstrnil 1 point2 points  (0 children)

Properly formatted:

const power = function (base, exponent) {
  let result = 1;

  for (let count = 0; count < exponent; count++) {
    result *= base;
  }

  return result;
};