Don't understand why my do/while code is not working


#1

In "to learn it you gotta 'do' it" exercise, it seems that my code is not working.

My code:

var getToDaChoppa = function(number){
    do {
    console.log("Batman");  
    }  while(getToDaChoppa < 3) {
      console.log("Superman");  
      getToDaChoopa ++;
  }
};
getToDaChoppa(0);

I wanted to print one Batman and 3 Superman, printing Batman with do and 3 Superman with while loop, but it tells me that the syntax is wrong, and it printed only one Batman and one Superman.

Could someone please tell me what is wrong with my code?

So far it seems that as long as I put the do/while in function, it either tells me "wrong syntax" or goes into an infinite loop.
The two codes below go into an infinite loop, and I don't understand why:

(1)

var getToDaChoppa = function(booleans){
    do {
    console.log("Batman");  
  }  while(getToDaChoppa) {
    console.log("Superman");  
    getToDaChoopa = false;
  }
};
getToDaChoppa(true);

(2)

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

Thank you!


#2

we have a while loop:

while(condition){

}

and a do while loop:

do {

} while(condition)

you combined a do while and while loop into a do while while loop, you can't do this. This gives the syntax error

the other important thing to understand are function parameters (number) and arguments (0):

# function declaration with parameter number
var getToDaChoppa = function(number){
    console.log(number);
}

# function call with argument zero:
getToDaChoppa(0);

see how the arguments gets copied into the function parameter? So i would use number for your loop condition, not the function object itself.

You should be able to improve this code now, if you need more help post updated version of your code

actually also covered this? use the function parameter, not the function itself


#3

hey @stetim94 Thank you very much for the clarification on the do/while loop!

So I think I misunderstood how the do/while loop can be used...

But may I just ask you, why do we use parameters instead of the function object in this case???

I put in parameters in the code below and yes it worked. :slight_smile: But why not function object?
var getToDaChoppa = function(booleans){
do {
console.log("Batman wins!");
} while(booleans);
};

getToDaChoppa(false);

On the other hand regarding the code below:
var getToDaChoppa = function(number){
do {
console.log("Batman wins!");
} while(getToDaChoppa < 3);
};

getToDaChoppa(0);

Why does it log only 1 "Batman wins!" instead of 3? I thought in this code, I let it run from 0, and stop when it approaches 3?
And when I put in function parameters (number < 3) instead of the object (getToDaChoppa < 3) it went into an infinite loop. why?

Thanks again!


#4

because its highly uncommon to use functions in loop conditions, function are used to execute code multiply times.

look:

var getToDaChoppa = function(){
    console.log("do something")
}
console.log(getToDaChoppa)

getToDaChoppa is a function as you can see in the output.

so now look at some your conditions:

while(getToDaChoppa)
# which translate into:
while([function])

weird condition, and its tricky to tell how JS will evaluate it. or this one:

getToDaChoopa ++;

increase function by one, how does that work?

Maybe you need a refresher on what function are suppose to do? (at this point in the course, we assume you know basic about function)

number will always be zero? You never increase it, so the condition can never becomes false


#5

@stetim94 Thank you very much for the function reminder! I just had that "Ah-ha!" moment :slight_smile:

However, is it even possible at all to use the do/while function to log "Batman wins!" 3 times?
Or it is possible only through the following code?

var click = 0;

while(click < 3) {
console.log("Batman wins!");
click++;
}

In the code above, "2" was mysteriously logged on the screen after my "Batman wins!" x3 although I cannot see anything causing that "2" to appear.

But if I put it in a function (like the one below) the "2" disappears and I don't even use parameters neither the function name in the while loop. Why is that?

var click = 0;

var loop = function(booleans){
while(click < 3) {
console.log("Batman wins!");
click++;
}
}
loop(false);

Thanks a lot I started to feel that if I ask more I 'll have to pay you haha :laughing:


#6

its console behavior, you update a variable after the last console.log, swap console.log and click++ around

if set the condition for do while right, you should be able to log to the console 3 times


#7

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