This forum is now read-only. Please use our new forums! Go to forums

0 points
about 12 years

iteration: what's that mean in coding language? I can't figure out how to make I++ equal to 2

in the section entitled “bring it up (and down)” and the directions read: “After the variable i is declared, call i++ twice to get i equal to 2. This should involve two separate lines with i++; on them. Try it out now.” and the hint reads: “Here you can have two separate lines of iteration. Each should contain just i++.”

What do they mean “call…” and “separate lines of iteration”?? I just don’t know what I’m trying to do.

I tried this:

var i = 0; console.log( “i is equal to “ + i ); var i++ = 2; var i++ = 2;

as well as just: var i = 0; console.log( “i is equal to “ i++ ); i++

I really am a total beginner with coding, and I’m lost. The lessons prior to these were the FIRST I’ve ever had with coding. I don’t really understand how to do this or what I’m trying to do… up until now, I think I’d understood the directions pretty well, but now I am lost :(

Thanks for any advice here.

Answer 4f45265441b1b1000300ceb7

2 votes

Permalink

The answer is to look at the order in which you’re doing things:

var i = 0;  /* you set i to zero */
console.log( "i is equal to " + i ); /* then you print out the current value, which is still zero */
i++;  /* then you increment i by 1 (add 1 to i) */
i++;  /* and then you do it again */

So at the end of the script, the correct value of 2 is in the variable i, you’ve just printed it before you’ve added to it. Can you see how you might fix that?

points
Submitted by Hannele Kormano
about 12 years

Answer 4f4013a7faaa8b0003023b3f

1 vote

Permalink

woops! of course, seconds after I post the question, I then figure it out. It’s the simplest thing in the world too! The correct code for this exercise is just:

var i = 0; console.log( “i is equal to “ + i ); i++; i++;

points
about 12 years

Answer 4f401555a5b1830003024503

0 votes

Permalink

only, actually, I take it back! codecacademy thinks I’m right, but the answer shown is actually 1 in this exercise as well as one in the decrementor exercise that follows. When the answers should be 2 and 0, respectively. So, although codecacaemdy indicates I should move on to the next exercise and gave me my points, something is not right!

any ideas?? the above code gets me “Ok-ed” by the academy, but does not give the values it said should get…

points
about 12 years

Answer 4f4376c48804d20003024a7d

0 votes

Permalink

i also don’t know.

points
Submitted by vashblink
about 12 years

Answer 4f499965df4f90000300b5c5

0 votes

Permalink

i++; means adding 1 to i or it equals to the statement i=i+1; it could say an iteration because it adding a value and return its result to itself.

on the scratch, we start with var i=0; by put statement i++; makes i equals to 1. an then we put i++; again so now i equals to 2. this is the objective.

that’s why we should put i++; twice to get i=2 at the end.

another similar statement:

i– equals to i=i-1 i+=n equals to i=i+n i-=n equals to i=i-n i*=n eqals to i=i*n i/=n equals to i=i/n i%=n equals to i=i%n

may it helps;

points
Submitted by Nasrul Hamid
about 12 years