Question about syntax


#1


I was able to figure this out but I am not exactly sure why it works the way it does.  I have noted it below.  Thanks 

var go = true;

var loop =function(){
    while(go) {console.log("Let's get going"); go = false;}

    



for (var i = 0; i < 20; i+=2) {console.log(i);}

}  //Why does bracket for the end of while loop go here and not on line 5 after the false;}?//
loop();


#2

You got separate things there.
1. you set your variable go to true. Your you have function called loop that has while and for loop nested in it.
while loop loops once because after 1. console.log it sets the value of variable go to false and that stops the loop. Because while only runs while it is set to true

  1. After a while loop did a 1 loop and it ended, now it came time to run for loop.
    For loop runs from 0 to 20, not including 20 and console.logs out ever 2 number because i+=2 goes up by 2 each looping.

All in all you have 1 function in which you have while and for loop that run when function is called.

var go = true;

var loop =function(){
    while(go) {
    	console.log("Let's get going"); 
    	go = false;
    }
    for (var i = 0; i < 20; i+=2) {
		console.log(i);
    }
}  
loop();

A more readable code from above


#3

Ok that makes sense. When I first started I was planning to separate the while loop and call the loop function then write the for loop, but this did not work and I was able to fix it by moving the closing bracket to the end. Is there a way to write these as separate? Does it require a var function to make it run the for loop independently from the while loop?


#4

You can write the same code just full remove the loop function and you will get the same output.
while and for loop are not connected and they don't affect each other directly.
1. while loop runs
2. after while loop is done running, for loop start to execute.

while loop executes its part of code and for loop executes its part of code


#5

Not sure I completely understand, I figured the for a while could be separated but I am not exactly sure how as I submitted the answer in what I thought was separate and it did not like it (syntax error). Would you be able to edit the code I have to show me how this might work. It is usually easier for me if I see it. Thanks!


#6

This code:

var go = true;

var loop =function(){ //start of a function on call
    while(go) { // while variable go is set to true, the loop will run
    	console.log("Let's get going"); // consol loged the string
    	go = false; // go is false and loop ends here
    }
    for (var i = 0; i < 20; i+=2) { //starting of for loop 
		console.log(i);
    } //ending of for loop after i comes to 20
}  
loop(); //calling the function which will execute all above as commented

Is the same thing in output as this code:

console.log("Let's get going"); 
    for (var i = 0; i < 20; i+=2) {
		console.log(i);
    }

and it is much easier to understand and write while having the same result


#7

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