5. Practice makes Perfect explanation?


#1


So, I managed to solve the Practice Makes Perfect problem, but I'm not really understanding what I wrote so I'm wondering if anybody could help me understand it a little bit better.

var count = 0

var loop = function(){
while(count < 3){
//Your code goes here!
console.log("I'm looping!");
count++
}
};

loop();

This was my code, but I'm not understanding why I need to put the count++ outside of the while(), when I've been putting them inside in the for loops. Also, what is the loop(); at the end?


Also, this is my first time, could anyone tell me how to edit this to make this more legible? Don't really get the bold fonts and the red texts.


#2

First of all, count++ is actually inside the while loop. (It's inside the body of the while). Also loop(); at the end is you actually calling the function. At the top you create the function, but to use it you have to call it.


#3

I just realized that loop(); is the variable's name. Thanks for explaining.

I still don't quite understand the count++ though, I've tried putting it in with the (count < 3) but they're telling me that's a syntax error.

What's different about for loops and while loops that's giving me that error?


#4

Okay so I will try my best to explain the while loop to you.
Basically, you are telling the program while the count variable is less than three, execute code. count++ in the body of the while increments count by one. Soon enough count won't be less than three, and the loop will finish.


#5

First I have to shout out labs repl.it. It is Codecademy’s beta lab where you can test your code. I immeditely plugged in what you have and it showed me the two line where your code was missing statement closers aka ‘;’. Think that lab space will be of great use to you!!


#6

My code is like this:

var count=0;
var loop = function() {
while(count<3,count++) {
console.log("I'm looping!");
}
};

loop(count);
Unable to log why?

var count=0;
var loop = function() {
while(count<3) {
console.log("I'm looping!");
count++;
}
};
loop(count);
I am able to log why? count++ is outside while loop and inside body why?


#7

I didn't know about the lab environment... Thank you for pointing that out. I am not picking this javascript stuff up very well. I'm a UNIX admin so I'm use to running for loops and while loops there, but it isn't the same and I think that is messing with me.

Take care


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.