6. solo flight - soloLoop(); at the end?


#1



https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/1/2?curriculum_id=506324b3a7dffd00020bf661#

my code works fine im just wondering what the soloLoop(); at the end is for

this is my code,

soloLoop = true;
var soloLoop = function(){
  while(soloLoop) {
      console.log("Looped once!");
      soloLoop = false;
  }
};

soloLoop();


Practice makes perfect
#2

Well functions are some sort of sub programs that do not run on their own but need a start command and that is the start command. On the other hand you can use them whenever you want and not just once. But maybe have a look at your condition, do you really feel comfortable with a function as a condition?


#3

soloLoop();

This is what is known as a function call it executes your function wherever you put the call.

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8&safe=active&ssui=on#q=function%20call%20javascript&safe=active&ssui=on


#4

ok that makes sense! need to look back at functions i guess. hurm yeah do you mean that i labelled the variable that equals 0 at the beginning and the function the same thing? i dont know why i did that.


#5

Yes you should try different variable names, as it goes:

sL = true
sL = function
while(sL) // still a function
sL = false
while(sL) // now false -> exit

PS: Just out of pure curiosity. If you'd call the function for a second time, does it break because now sL is false and no longer a function? :slight_smile:


#6

eheh i guess i just wasnt really paying attention there, makes sense !


#7

ooh heres another question

if you get rid of the function part the code still works, it just prints out the number of times its looped afterwards too? why is that? for example,

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

loop();

becomes

while(count < 3){
		console.log("I'm looping!");
		count++
	}

what they print is similar, so what is the function doing?


#8

You're running it in a console so it repeats the last used value which is this:

loop();

in the first case. Which has a "value" of undefined (no value) as nothing is returned. And this:

count++

in the second case.


#9

soloLoop() is calling the argument.....You should go through the "function" part once...then you'll understand