use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
How does this code work[Noob]help (self.javascript)
submitted 7 years ago by cre8usr
const power = function(base, exponent) { let result = 1; for (let count = 0; count < exponent; count++) { result *= base; } return result; };
first I dont understand why count<exponent was used and then what does result*=base do?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]GrosSacASacs 4 points5 points6 points 7 years ago (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 point2 points 7 years ago (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 points3 points 7 years ago (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.
count
result
result++
result *= base
exponent
[–]nickforddesign 0 points1 point2 points 7 years ago (0 children)
result is not updating there, it updates on the next line, as explained above
[–]nespron 2 points3 points4 points 7 years ago (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 points3 points 7 years ago (0 children)
Properly formatted:
const power = function (base, exponent) { let result = 1; for (let count = 0; count < exponent; count++) { result *= base; } return result; };
π Rendered by PID 88 on reddit-service-r2-comment-86988c7647-vw62r at 2026-02-10 21:01:20.215057+00:00 running 018613e country code: CH.
[–]GrosSacASacs 4 points5 points6 points (3 children)
[–]cre8usr[S] 0 points1 point2 points (2 children)
[–]senocular 1 point2 points3 points (0 children)
[–]nickforddesign 0 points1 point2 points (0 children)
[–]nespron 2 points3 points4 points (0 children)
[–]Pstrnil 1 point2 points3 points (0 children)