This forum is now read-only. Please use our new forums at discuss.codecademy.com.

169 points
51351527caed3b07890020ec_634748697
Submitted by
Jeffrey Fuller
over 5 years ago

What do I do from here.

I assumed that the system just wanted to check my syntax, but apparently I must add a bit more to this exercise to complete it. I've tried using some of the examples, and I get an infinite loop.

This is what I entered:

**var condition = function() {

};
while(condition) {
console.log("i'm looping")
}
for(i = 0); i < 0; "!") {
console.log("please stop looping")
}
var condition();**

I really have no clue as to what I should do next.


6 votes

permalink

Ok, I think you are jacking with me here, but I will bite, just in case you are not :)

var condition = function() {

};

Here you are creating a function called condition. Usually you stuff code inside the function. { contains the code for the function }. You have not put any code in this function.

var condition();

Jumping to the bottom. Here it looks like you are trying to call your function, and you are not! The keyword var is used to create new variables (variables/arrays/functions, etc). The interpreter is lost at this point, because you are creating a new function and doing it incorrectly. To call the function you created above, you would do it without var:

condition();

This will run the code inside your function. Remember that your function has no code, so this would not do anything.
+++++++++++++++++++++++

while(condition) {
console.log("i'm looping")
}

Naming your function condition was a bad idea, as it is confusing. Here it would be nice to have a variable named condition, and this is also where your infinite loop is running wild. while(condition) is like saying while(i have a variable named condition, print i'm looping, forever and ever). You do have a variable name condition and it is a function.

We could add a line to stop the infinite loop:

while(condition) {
console.log("i'm looping")
condition=false;
}

This code is not correct as condition is suppose to be a function (something containing code) and we are just setting it to false. At least it will break us out of the infinite loop. Pretend the function was named something else and we could have:

var condition = true;
while(condition) {
console.log("i'm looping");
condition=false;
}

Booyaoh! let's move on.
+++++++++++++++++++++++

for(i = 0); i < 0; "!") {
console.log("please stop looping")
}

I'm not much of a programmer, and as far as I can tell, this is just jacked up code.
for(i = 0); i < 0; "!") {
First the ) after 0, does not belong:
for(i = 0; i < 0; "!") {
The "!" is incorrect. Before trashing it lets look at this line. We set i to 0. Then we say, as long as i is less than 0. If i is 0, then I can never be less than zero. So this code does not run, it is not your infinite loop. Ok. back to "!". It does not go here. i++ or i-- can go here. Let's see a it in action:

for (var i=5; i>0; i--){
    console.log("please stop looping");
    console.log(i);
}

var means create a variable, so this says, create a variable named i and set it's value to 5, as long as i is greater than 0, print please stop logging me, then subtract 1 from i.

Now during a comparison you can use ! but it means not, and is usually used in an if statement. Like:
if (i != 3) { console.log("but i like 3");}
This says: if i is NOT 3 spit out: but i like 3.
+++++++++++++++++++++++

How about some coodies:

// Let us create a variable to be used as a condition
kryptonite = true;

// Let us create a function called superwoman
var superwoman = function() {

  // Let's add an if statement and use our pretty kryptonite variable.
  // We can mix it up and use !== to represent "not false" or true if you will.
  if (kryptonite !== false) {
      console.log("Smack!! Superwoman just tried to fly with kryptonite in her pocket");
      console.log(""); // blankety blank line
  }  // ends our if statement

  // a while loop using are lovely conditional variable again
  while (kryptonite) {
    console.log("Superwoman ain't so tough!!!");
    console.log(); // Print a blank line
    kryptonite = false;
    // kryptonite is false, she's tough again, and broke out of our while loop!
    // Shhh! don't tell lexanne lutheretta...
  } // close while loop

  // printing inside function but outside the while and for loops
  console.log("Look! It's a bird!");
  console.log("No! It's a plane!");
  console.log("No...! \n"); // \n is another way to print blank line (newline)

  // for loop, we can use i+=1, instead of i++ if that is confusing
  for (var i=0; i<2; i+=1){
    console.log("It's SUPERWOMAN!");
  } // close the for loop

}; // close function superwoman

// None of the code above will run yet, because it is all inside the function.
// We have to call the function first:

superwoman();

omg!! run she's loose!!!

If you go to:
http://labs.codecademy.com/
and choose javascript, you can paste the code above, if you want to run it.

641 points
Ff597994808d1886eb06fa495c5eecd8?s=140&d=retro
Submitted by
Envia Parsons
over 5 years ago

1 Comment

5181cd196ce85fbada001c2d_702559395 michi-tech over 5 years ago

Awesome example with superwoman:)


0 votes

permalink

Thanks for your response, this is very helpful, and no I'm not pulling your leg, I was really confused.

169 points
51351527caed3b07890020ec_634748697
Submitted by
Jeffrey Fuller
over 5 years ago