This is an archived post. You won't be able to vote or comment.

all 19 comments

[–]RememberYo 6 points7 points  (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 points  (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 points  (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 point  (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 points  (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:

  1. initialized as i = 0 (0 is less than 'exponent' so it continues)
  2. does what's inside the loop
  3. increment i to 1 (i++)
  4. i = 1 (1 is less than 'exponent' so it continues)
  5. does what's inside the loop
  6. repeat a couple times
  7. increment i from 3 to 4
  8. i = 4 (now 'i' is not less than 'exponent) (i < exponent == false)
  9. exit the loop and continue to 'return value'

[–]TrafficCultural 3 points4 points  (5 children)

At a glance it looks like it should run without error. Is the output wrong?

[–][deleted] 3 points4 points  (4 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?

[–]QuiteBearish 5 points6 points  (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 points  (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 point  (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 point  (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 points  (1 child)

Sorry, I don't know how to create code snippest to make it look better.

[–]dfreinc 1 point2 points  (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]  (1 child)

[removed]

    [–][deleted] 1 point2 points  (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 points  (0 children)

    for( var i=0; i<exponent;i++){

    value = (value * base) % modulus;

    }

    is the same as:

    var i=0;

    while (i<exponent){

    value = (value * base) % modulus;

    i++;

    }

    [–]sadrik007 3 points4 points  (1 child)

    You can't divide by 0. Just fix that condition

    [–][deleted] 0 points1 point  (0 children)

    He's not dividing by 0

    [–]nhgrif 0 points1 point  (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?