Solo Flight


#1

I seriously don't even know where to start on this. I am just very confused. Can someone please thoroughly explain the topic? Here it is:

//Remember to make your condition true outside the loop!
var myCondition = true;

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

};

The instructions are:

Instructions
Inside the soloLoop function, write a while loop that takes an initial condition that's true. Your loop should log "Looped once!" to the console, then change that initial condition to false.

MAKE SURE to set your condition to false in the body of your loop. Otherwise, you'll loop forever!


#2

//Remember to make your condition true outside the loop!
var myCondition = true;

var soloLoop = function(){
     while(myCondition){
       console.log("Looped once!");
       myCondition = false;
     }
};
  1. variable myCondition is set to true
  2. variable soloLoop is assigned a function
  3. within the function, there's a while loop
  4. the loop states: while the condition is equal to true ,
    (or while myCondition is true)
  5. print "Looped once!" to the console, and
  6. set myCondition to false
  7. If the condition becomes false, it will skip the code block
    (which is to print the string to the console, and set the variable myCondition to false)
  8. Since the while loop is inside a function, which is assigned to soloLoop, which is never called...the code never runs.

Inside the soloLoop function, write a while loop that takes an initial condition that's true. Your loop should log "Looped once!" to the console, then change that initial condition to false.

Add the following to the bottom of your code to call the function:

soloLoop( );

Another alternative would also be to not create any variable called myCondition, and just pass the value within the function call:

var soloLoop = function(parameter){
     [...]
}
soloLoop( true );

The following code would run the same way, without ever using the variable "myCondition" (which therefore can be deleted):

//Remember to make your condition true outside the loop!
var myCondition = false;

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

soloLoop( true );

Notice how myCondition still goes through with the code, even though the var myCondition is set to false from the beginning. The reason for this is because the "myCondition" passed inside the function is what is called a parameter, which takes its value from what is called an argument passed through the function call, soloLoop( value ). In this case, the argument, or the value, is true. The variable myCondition is something completely different, and is never used.

So really, we should just delete that from the code above:

//Remember to make your condition true outside the loop!
var soloLoop = function( myCondition ){
     while( myCondition ){
       console.log("Looped once!");
       myCondition = false;
     }
};

soloLoop( true );

Hope that helps more than it confuses you. If you have any questions, just ask.


#3

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