10/11 - To learn it, you gotta do it


#1

So my code is running (thanks to the great advice of the forum posters!) but I don't understand one thing: what is the purpose of "while (false)" in this code?

var getToDaChoppa = function(){
var count="shaurya";
do {
console.log("my name is" +" "+count);
}
while(false)
};

getToDaChoppa();


#2

@codecoder81953

The do loop will first execute the code between the code in the curly brackets once. After, it will check the condition in the while loop if it's true, it will repeat the loop. Otherwise, the loop stops running here.


#3

You seemed to know what you are doing: What is wrong with my code?
var getToDaChoppa = function(){
// Write your do/while loop here!
do {
console.log(" GET TO DA CHOPPA!")
}
}; while (getToDaChoppa)

getToDaChoppa(true);

It says Syntax error unexpected token }


#4

Here is the fault with your code (the solution below):

This needs to be INSIDE the function. Your current code puts it outside the function.

Remember, when writing a do loop, the while loop has to BE A PART OF the loop as a way to limit it. Therefore you also need to write --> while(false) to make sure it runs only once. This is important otherwise the do loop will keep running without any end.

Final code should be this:

var getToDaChoppa = function(){
do {
console.log(" GET TO DA CHOPPA!")
} while (false)
};

getToDaChoppa();

This basically means that getToDaChoppa() starts the function, running the do loop which prints "GET TO DA CHOPPA!", finally at the end, the while loop tells it that it doesn't need to run the function anymore, therefore the function stops running.


#5

Thanks this was very helpful!