Javascript function with computing power(noob question using for loop);

This is the small function, it is from the book Eloquent Javascript. Page 30. I understand how computing powers work etc. What I am having trouble with here is trying to see what each piece of code is doing, been staring at it , reworking it and hoping something clicks, sadly no luck. Main part I am curious about is how the base continues multiply itself with itself 10 times(the exponent) . I am assuming it is done with the for loop count, but It is just not clicking.

Does the for loop count++ run once, then result *=base happen, then it returns, then continues that step 10 times? So var result = 1 after first pass through the function body, then it would be 2, then 4, then 8, then 16, then 32, then 64 etc all the way to the final result at 1024?

Gonna try to reword my above paragraph , just in-case some people don’t get what I am asking.

 Basically I'm curious if the body runs once through (and only one count++) then it goes through again, and again until the for loop condition is met?

Thank you for help. Hope I described my thoughts properly, sometimes hard to do when you aren’t sure exactly what to ask .

function power(base, exponent) {
    var result = 1;
        for(var count = 0;count < exponent; count++)
            result *= base
            return result;

    power(2, 10);

Hi davep1234. Welcome to the forums. :slight_smile:

I’m a beginner at this too, but I’ll give my take on it, and then someone better will chip in!

As I understand it, when the browser sees power(2, 10); it says, O, I need to run the power() function, so it plugs the two arguments (2, 10) into the power() function and then looks at what it needs to do.

It sees a for() loop, so knows that it will have to do the next operation a few times at least. It understand that count = 0 and that 0 is less than 10 (the exponent value) and so multiplies the result (1) by the base, which is two, and gets 2. It then takes that value of 2 and goes through the process again—now with count = 1, because count++ increased it by 1 in passing, which it keeps doing at each pass; and this process continues until count is no longer less than 10, at which point the function stops processing the for loop. By that time, result is up to 1024, so the function returns 1024 as the result.

That’s my take on it, anyhow. :slight_smile:

Hi there,

Welcome to the forums.,

First off, it would help to format the code properly, as otherwise it seems as if the closing curly bracket belongs to the for loop, when in fact it belongs to the function.
To avoid this confusion, I prefer to use curly brackets for for loops, too.

function power(base, exponent) {
  var result = 1;
  for (var count = 0; count < exponent; count++){
    result *= base;
  return result;

console.log(power(2, 10));

> 1024

Your question seems to relate to the way the for loop works.
Basically this will run as long as the condition count < exponent is true.
count is incremented by one each time the code within the loop executes.
Within the loop result is multiplied by base and the total is added back to result.
This is the same as writing result = result * base

Does that help?


Beaten to it :slight_smile:

It sure is easier to read like that. :slight_smile:

That makes sense now, crazy how some curly braces can change the perspective a ton. ( book has no curly braces) . I was so confused if the for loop made the whole function body run again(which was never mentioned and I was under impression that’ not how it worked. I’m gonna make sure to use those braces from now on. Thank you very much:).

Ya for sure, I was completely confused without those braces.

Once again ralph, thank you so much. The small details is what I find hard with javascript. I generally do okie at learning new things but this langauge is kinda getting to me. Small advice like what you did moves me forward fast…

Also, if you do see this message… Is it possible to ever get a job running html css and java? I hear from some people I know that it is hard. I for one don’t want to accept that. Thank you

I assume you mean JavaScript, which is totally different. Anyhow, if you get really proficient in all those three, you have a very powerful arsenal of skills, so I have no doubt it’s a great platform for employment. JS in particular is exploding, and has uses way beyond the web, so it’s a really “happening” thing, as they say. :slight_smile: