10/11 - Cannot figure it out, please help


#1

Hello,

I have submitted the coding below for part 10/11 and i keep getting the following error message 'SyntaxError: missing formal parameter'.

var getToDaChoppa = function(true){
do {
console.log("It's " + getToDaChoppa + "that i'm gonna get to da choppa!");
}
while(true);
};

getToDaChoppa(false);

Thank you in advance!


#2

Hi, true is reserved keyword. Yo may try

var bool =true;
var getToDaChoppa = function(bool ){

But this code produces a infinite-loop.


#3

Thanks for that but as you mentioned it produces an infinite loop.
So instead i tried the following and it actually worked:

var getToDaChoppa = function(){
var getToDaChoppa = true

do {
console.log("It's " + getToDaChoppa + " that i'm gonna get to da choppa!");
}
while(getToDaChoppa = false)
};
getToDaChoppa();

The only thing about this i find confusing is i thought the while command was meant to be true. As i'm asking "Do" this "While" this is happening.


#4

Maybe have a look at this post on how do while loops work:

Two things about this code are really bad habits. First of all its never a good idea to use the function name as a variable inside your function as you did here:

var getToDaChoppa = function(){
var getToDaChoppa = true

As @pointphoton suggested you may name it bool or anything else but please not the same name as the function. Because with this line you make it impossible to use the getToDaChoppa function inside of the function again. Ok you don't do this here still it is a bad habit as you might later use want to use recursions (calling a function inside itself). It could have been worse by not using the var because then you would not only make it locally unavailable but would overwrite the whole function. So again pls there is an almost infinite amount of possible names choose another one for another variable.

Also this is not the best stopping condition:

while(getToDaChoppa = false)

Do you know what it does and why?

For while loops in genereal:
The idea is that the stuff after do is executed at least once then the condition is checked and if it is true you loop by jumping back to do and if it is false you exit the loop. Of course here it doesn't make sense to use a loop if you don't loop but I guess it's more to show this feature of running the code at least once. Also yes the condition needs to be true in order to really loop but after some time you want to stop so always make this condition depend on a variable that you switch after some time or count up till a limit but really avoid booleans as true and false, as false is pointless (no loop) and true is dangerous (infinite loop).


#5

I'm on 10/11 as well but none of the explanations are helping me.

On 9/11 I created a do/while loop aside from the one that the exercise asked for. The do/while loop worked fine but for some reason I cannot figure out how to interact with the function.

Before anyone asks, I read all the relevant links such as FAQs and other explanations but none of it is clicking for me.

Below is my work for exercise 9 that I mentioned above:
var loopCondition = false;

do {
console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");
} while (loopCondition);

do {
console.log("Print out the loop condition = " + loopCondition);
} while (loopCondition);


#6

Well the function syntax is:

var name = function(){
    stuff to do
}

name();

and where I wrote stuff to do, your do while function should be placed. That is pretty much it if you adjust the name placeholder to the name of your function this should probably be all you need.