Question 10/11 "To learn it, yo gotta "do" it!"


#1

Hi, I have tried this exercise multiple times, but every code that I write I always get an error.

var getToDaChoppa = function(false){
do {
console.log("You've just been pwned!");

};
getToDaChoppa = true;
while(getToDaChoppa());

Could someone please tell me if it is my syntax or my code construction, or if the entire code is wrong.
Thank you in advance.


#2

Hey @mc_yemi_ako! These are your errors first of all you forgot to close your brackets of the function. So i f we get that out of the way second you dont call a function by doing while(functionname()); Its like this getToDaChoppa(false);

Last of all in the specific ecercice its asking you to put it in all together like this:

        var getToDaChoppa = function(){
          do {
        console.log("You've just been pwned!");
        getToDaChoppa = true;
           } while(something);
        };
        // And then as i stated above this is how you call the funcion.
        getToDaChoppa();

#3

You need to write your do while loop inside the function:

var getToDaChoppa = function(){
  do {
    // do something 
   } while( some condition);
};
// call function
getToDaChoppa();

no need to pass arguments in the function, you still need to write the condition of the loop


#4

@stetim94
@amanuel2

This is the code I passed in:

var getToDaChoppa = function(){
do {
console.log("Congrats! You are becoming good at programming!");
} while(getToDaChoppa);
};

getToDaChoppa(true);

But my browser keeps on crashing...:confused:


#5

Why do you pass an argument in your function? Don't, just make a variable and set it to true:

var getToDaChoppa = function(){
 my_variable = true;
  do {
      console.log("Congrats! You are becoming good at programming!");
     // set my_variable to false
      } while(my_variable);
};
getToDaChoppa();

you still need to set my_variable to false in the loop, otherwise you have a infinity loop. (just like you have now)


#6

@stetim94
Wow. Thanks. I never thought I would need to add in an extra variable. Thanks!


#7

You could do this with passing an argument into the function, here is an example:

function myFunction(x){
    do {
        console.log("hello world");
        x = false;
    } while (x);
};
myFunction(true);

now, you give the function an argument (i called it x, i wouldn't call it true of false), then when i call the function, i give x a value of true, then in the loop i set x to false. You could it this way as well.


#8

I tried both of them, they are correct. Thanks for helping me along my coding journey!


#9

Do you also understand why they work? That is most importatn


#10

Correct me if I am wrong but the (true) in this case will make the solution infinite and the false eventually stops the equation after the solution is found? Just like the heads & tails lesson earlier on? I also dream to be a great coder some day, so please help me understand a bit clearer :smile:


#11

Which code are we talking about? i see at least 5 answers with code. And none of them a infinity loop.

What is the head and tails exercise (link)? I don't know every exercise by name, and really time consuming to through the entire javascript course.

Very good, trying to understand is a good skill to have as a programmer :smile:


#13

stetim94 can you explain the 'my_variable' part of the code? I don't where it comes from or what is suppose to relate to?

var getToDaChoppa = function(){
my_variable = true;
do {
console.log("Congrats! You are becoming good at programming!");
// set my_variable to false
}while(my_variable);
};
getToDaChoppa();

you still need to set my_variable to false in the loop, otherwise you have a infinity loop. (just like you have now)


#14

Variable should be covered at this point in the course, so should booleans. The loop will run, as long as my_variable is true:

while(my_variable);

Well, your variable my_variable is true, since you defined that on line 2:

my_variable = true;

Now you need to make it false in the loop (set the my_variable to false), otherwise the code will run forever


#15

If you are stuck on this problem and your browser keeps crashing, it is probably because you are in an infinite loop. Check your parameters and look closely at where your variables are declared.

var getToDaChoppa = function(){
// my sentence will print once, then my variable decision will be set to false, then my while loop will check for the condition to see if it is true (it is false) so the loop is not run and the function ends.
do {
console.log("Deploy thousand-year back hair!");
decision = false;
} while(decision);
};

getToDaChoppa();


#16

Will this code work? I don't want to crash my browser! :sweat_smile:

var myCondition = false;
var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("Will this " + myCondition + "loopCondition work?");
} while (myCondition);
};

getToDaChoppa();

#17

Yes, that will work. the do section execute once, then the while condition gets checked, which is false, loop ends, function ends, you pass the exercise


#18

thanks @stetim94 ! :smile:


#19

I got this to work but I'm not understanding it.
var getToDaChoppa = function(a){
do {
console.log("test");
a=false
} while (a);
};

getToDaChoppa(true);


Why does it need var getToDaChoppa = function(a){ and getToDaChoppa(true);? What are these lines doing?


#20

well, your function takes in an argument (a), when you call this function, you pass a value to this argument, you can see this:

var getToDaChoppa = function(a){
  console.log(a)
  do {
      console.log("test");
      a=false
  } while (a);
};

getToDaChoppa(true);

As you can see, a is true, since you define that in your function call:

getToDaChoppa(true);

This allows you to pass argument (seem them as variables, into your function so function can behaved different depending on the arguments you feed the function when calling the function


#21

Thanks, this is helpful. It still seems strange to me that you use the parameter (a) instead of the variable. My brain wants to write
} while (getToDaChoppa);, since that is the variable.