This forum is now read-only. Please use our new forums! Go to forums
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){
//Your code here!
console.log("I'm looping!");
}
};
loop();
Answer 5127c3411286146e22000250
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
buy one apple
take it home
count the apples
}
Now go take a rest. Good robot.
Answer 5127c7aa42949f7d70000551
//Remember to make your condition true outside the loop!
var apples = 0;
var loop = function(){
while(apples<3){
//Your code here!
console.log("I'm looping!");
apples += 1;
}
};
loop();
1 comments
That’s perfect :)
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
4 comments
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?
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
oh, btw: the comments don’t allow code formatting or any HTML. They’re plain text only.
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