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

0 points
almost 11 years

# counter variable in the body of the loop

I am starting to struggle a lot. But I don’t want to give up yet. Give me a hint what is meant by “counter variable in the body of the loop”, but don’t give me a full answer please. Maybe suggest one of the previous javascript exercises that teaches counter variable? My mind is totally blank. Here is the code:

``````//Remember to make your condition true outside the loop!

var loop = function(){
while(something){
console.log("I'm looping!");
}
};

loop();
``````

Remember to make your condition true outside the loop!

What Eric means by “condition” here is the loop condition, which is what you put into the parentheses after `while`. So for instance, you could say

``````var x = true
while (x) {...}
``````

But now, depending on whether you change the value of `x` inside the `{` loop body `}` or not, your loop is either going to repeat forever (that happens if you don’t change `x` inside the loop, of course, so the loop condition remains `true`), or run only once (if you change `x` inside the loop to be anything other than `true`).

But what you need is three loops repetitions. So the idea here is to introduce a counter variable, change that counter inside the loop, and have the loop condition compare the counter to some pre-set value, so that the comparison will be `true` three times, no more, no less.

Whenever you have trouble writing an algorithm, remember that it’s a set of instructions (so it’s something you might give to a very stupid robot (which is essentially what your computer is)).

For this looping scenario, for instance, you can imagine a robot that can count and go to the store to buy things, but can only carry 1 thing at a time. How would you make that robot go to the store exactly three times? Well, simple enough (pseudocode):

``````I have zero apples in my home.   // *hint* loop counter! *hint*

Tell robot:
while (I have less than three apples in my home):  // condition!
{   go to the store
take it home
count the apples
}
Now go take a rest. Good robot.
``````
points
Submitted by Alex J
almost 11 years

Hey, I actually cheated on this one and looked what others have done. However I think I understood more about javascript after I did several following lessons. Does the code below (I don’t know how to format text in direct comments) satisfy you or do you want all these commands (go to the store, buy one apple, etc…) printed in console?

Alex J almost 11 years

you must be witty… and your solution doesn’t need to satisfy me at all, I didn’t write that exercise and I don’t work for Codecademy. But I’m glad I was able to help

Alex J almost 11 years

oh, btw: the comments don’t allow code formatting or any HTML. They’re plain text only.

Alan Reid almost 11 years

You see if thse examples could be put in EVERY execise, I am certain people would learn faster. It puts the exercise inside thinking we understand

``````//Remember to make your condition true outside the loop!
var apples = 0;
var loop = function(){
while(apples<3){
console.log("I'm looping!");
apples += 1;
}
};

loop();
``````

exercise url

points
almost 11 years