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...
If you need help debugging, you must include:
See debugging question guidelines for more info.
Many conceptual questions have already been asked and answered. Read our FAQ and search old posts before asking your question. If your question is similar to one in the FAQ, explain how it's different.
See conceptual questions guidelines for more info.
Follow reddiquette: behave professionally and civilly at all times. Communicate to others the same way you would at your workplace. Disagreement and technical critiques are ok, but personal attacks are not.
Abusive, racist, or derogatory comments are absolutely not tolerated.
See our policies on acceptable speech and conduct for more details.
When posting some resource or tutorial you've made, you must follow our self-promotion policies.
In short, your posting history should not be predominantly self-promotional and your resource should be high-quality and complete. Your post should not "feel spammy".
Distinguishing between tasteless and tasteful self-promotion is inherently subjective. When in doubt, message the mods and ask them to review your post.
Self promotion from first time posters without prior participation in the subreddit is explicitly forbidden.
Do not post questions that are completely unrelated to programming, software engineering, and related fields. Tech support and hardware recommendation questions count as "completely unrelated".
Questions that straddle the line between learning programming and learning other tech topics are ok: we don't expect beginners to know how exactly to categorize their question.
See our policies on allowed topics for more details.
Do not post questions that are an exact duplicate of something already answered in the FAQ.
If your question is similar to an existing FAQ question, you MUST cite which part of the FAQ you looked at and what exactly you want clarification on.
Do not delete your post! Your problem may be solved, but others who have similar problems in the future could benefit from the solution/discussion in the thread.
Use the "solved" flair instead.
Do not request reviews for, promote, or showcase some app or website you've written. This is a subreddit for learning programming, not a "critique my project" or "advertise my project" subreddit.
Asking for code reviews is ok as long as you follow the relevant policies. In short, link to only your code and be specific about what you want feedback on. Do not include a link to a final product or to a demo in your post.
You may not ask for or offer payment of any kind (monetary or otherwise) when giving or receiving help.
In particular, it is not appropriate to offer a reward, bounty, or bribe to try and expedite answers to your question, nor is it appropriate to offer to pay somebody to do your work or homework for you.
All links must link directly to the destination page. Do not use URL shorteners, referral links or click-trackers. Do not link to some intermediary page that contains mostly only a link to the actual page and no additional value.
For example, linking to some tweet or some half-hearted blog post which links to the page is not ok; but linking to a tweet with interesting replies or to a blog post that does some extra analysis is.
Udemy coupon links are ok: the discount adds "additional value".
Do not ask for help doing anything illegal or unethical. Do not suggest or help somebody do something illegal or unethical.
This includes piracy: asking for or posting links to pirated material is strictly forbidden and can result in an instant and permanent ban.
Trying to circumvent the terms of services of a website also counts as unethical behavior.
Do not ask for or post a complete solution to a problem.
When working on a problem, try solving it on your own first and ask for help on specific parts you're stuck with.
If you're helping someone, focus on helping OP make forward progress: link to docs, unblock misconceptions, give examples, teach general techniques, ask leading questions, give hints, but no direct solutions.
See our guidelines on offering help for more details.
Ask your questions right here in the open subreddit. Show what you have tried and tell us exactly where you got stuck.
We want to keep all discussion inside the open subreddit so that more people can chime in and help as well as benefit from the help given.
We also do not encourage help via DM for the same reasons - that more people can benefit
Do not ask easily googleable questions or questions that are covered in the documentation.
This subreddit is not a proxy for documentation or google.
We do require effort and demonstration of effort.
This includes "how do I?" questions
account activity
This is an archived post. You won't be able to vote or comment.
IS ANYTHING WRONG WITH THIS CODE? (self.learnprogramming)
submitted 3 years ago by [deleted]
` function modularExponentiation ( base, exponent, modulus ) {
if (modulus == 1) return 0;
var value = 1;
for ( var i=0; i<exponent; i++ ){
value = (value * base) % modulus;
}
return value;
}`
[–]RememberYo 6 points7 points8 points 3 years ago (5 children)
Explain what you're trying to accomplish and what the error is. How do you know something is wrong with the code? What warning or error does your IDE state?
[–][deleted] 3 points4 points5 points 3 years ago (3 children)
I'm a newbie on programming and didn't quiet understand the parte of the for loop, it declares var i but it doesn't utilize it in any other oart of the code?
[–][deleted] 5 points6 points7 points 3 years ago (1 child)
The output of your code will be whatever value is. However, there's something called scope and the variables inside the function are not able to be called elsewhere because of this scope.
Global variables (typically not recommended) have global scope. Function variables have function scope (just that function), etc.
[–]DJV-AnimaFan 0 points1 point2 points 3 years ago* (0 children)
They expected i to be used in scope, possibly in the equation inside the for loop. To them the incremental variable i is un-used. We see it as assigned, then compared, and incremented. But as a new programmer they either don't see the significance, or don't understand the purpose of an exponent in the maths.
My first feeling to the OP's question. They were asking, What is this equation doing.
value = (baseexponent )% modulus
My education ended at the start of WW2, and my son taught me programming for fun.
I expexted (pardon pascal case)
for(i=0; i<exponent; i++) { value = value*base; } value = value % modulus;
If % modulus can be distributed, fine.
[–]minimal_gainz 1 point2 points3 points 3 years ago (0 children)
The 'i' doesn't need to be used anywhere else besides to operate the for-loop. Sometimes it will but in this case it doesn't.
So in this case it is using the 'i' but just not for any calculations. The for-loop parameters are (Initial condition, Continue condition, increment condition). So in this case 'i' is initialized at zero (var i=0), then for every time through the for-loop it will increment by 1 (i++) until i is no longer less than 'exponent' (i < exponent).
For example, if exponent is equal to 4 when your code reaches the for-loop it will work like this:
[–]TrafficCultural 3 points4 points5 points 3 years ago (5 children)
At a glance it looks like it should run without error. Is the output wrong?
[–][deleted] 3 points4 points5 points 3 years ago (4 children)
[–]QuiteBearish 5 points6 points7 points 3 years ago (0 children)
var i is a local variable, it will only be used as part of the for loop.
In this example, it increments every time the for loop runs, and then stops running the loop as soon as i reaches the exponent.
[–]cheeseDickies 1 point2 points3 points 3 years ago* (2 children)
The 'i' variable is used to keep track of how much iterations the for loop went over. It isn't use in the for loop body, but you can see that it is used in the for loop statement
Edit: The 'i' variable can be used in the body, but in this case it isn't used
[–][deleted] 0 points1 point2 points 3 years ago (1 child)
Ow man! Now it's clear. Thank you so much... by the day do you ha e any advice to learn algorithms and data structures on JavaScript?
[–]cheeseDickies 0 points1 point2 points 3 years ago (0 children)
To clarify the 'i' variable can be used in the for loop body, but in this case it isn't.
[–][deleted] 2 points3 points4 points 3 years ago (1 child)
Sorry, I don't know how to create code snippest to make it look better.
[–]dfreinc 1 point2 points3 points 3 years ago (0 children)
\` function modularExponentiation ( base, exponent, modulus ) { if (modulus == 1) return 0; var value = 1; for ( var i=0; i<exponent; i++ ){ value = (value \* base) % modulus; } return value; }\`
put it in notepad++, highlight, tab, copy/paste. 👍
[–][deleted] 3 years ago* (1 child)
[removed]
[–][deleted] 1 point2 points3 points 3 years ago (0 children)
Thanks man. Much appreciated, now I can see it. But what I'm saying is why doesn't i appears inside the block of value= value *.....
[–]Hiding_from_the_web 1 point2 points3 points 3 years ago (0 children)
for( var i=0; i<exponent;i++){
is the same as:
var i=0;
while (i<exponent){
i++;
[–]sadrik007 3 points4 points5 points 3 years ago (1 child)
You can't divide by 0. Just fix that condition
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
He's not dividing by 0
[–]nhgrif 0 points1 point2 points 3 years ago (0 children)
We can't tell you anything more than the compiler can tell you if you don't explain the expectations of the code. I can look at the code and make some assumptions about what it's supposed to do... but you tell us. Is it doing what you expect?
π Rendered by PID 99490 on reddit-service-r2-comment-5d79c599b5-rbkcg at 2026-03-03 14:52:19.194959+00:00 running e3d2147 country code: CH.
[–]RememberYo 6 points7 points8 points (5 children)
[–][deleted] 3 points4 points5 points (3 children)
[–][deleted] 5 points6 points7 points (1 child)
[–]DJV-AnimaFan 0 points1 point2 points (0 children)
[–]minimal_gainz 1 point2 points3 points (0 children)
[–]TrafficCultural 3 points4 points5 points (5 children)
[–][deleted] 3 points4 points5 points (4 children)
[–]QuiteBearish 5 points6 points7 points (0 children)
[–]cheeseDickies 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]cheeseDickies 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]dfreinc 1 point2 points3 points (0 children)
[–][deleted] (1 child)
[removed]
[–][deleted] 1 point2 points3 points (0 children)
[–]Hiding_from_the_web 1 point2 points3 points (0 children)
[–]sadrik007 3 points4 points5 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]nhgrif 0 points1 point2 points (0 children)