To learn it, you gotta 'do' it


Hi everybody. I was learning the do-while loops and tried to create a situation that forced the whille loop not to occur. Can you guys tell me if I did a mistake or if it's just a bug on the code of that exercise?
[on the console, it showed both X and Y]

var getToDaChoppa = function(number){
  // Write your do/while loop here!
      console.log ("X");
  while (1 > 2)
      console.log ("Y");

getToDaChoppa(6);<img src="//" width="690" height="370">


Getting X and Y on the console is right.

For the do loop, it obviously will print the string, X; while indeed, 1 is greater than 2, it prints Y.

Can you be more specific with your thoughts. Console is giving out what you gave it really.


I thought that, just by putting something something incorrect in the while loop (1 > 2 or 3%2=0), it wouldn't show me Y


Yeah... Once the condition you put in "while" is taken to be true or satisfied.

while (condition);

Just in case you are thinking there's a bug, which isn't the case, see this: JavaScript - To learn it, you gotta ‘do’ it


But the condition he put in while is false. 1 isn't greater than 2. I'm also curious why Y is printing.


I think I understood it, now. I thought that the do statement would make only the X to be printed. Now it makes more sense, to me, to have a _do/while _ loop for some cases. Thanks, man (and also, thanks for the link; I didn't know it until now).


I'm not totally sure, but I think that Y is printing only because the do exists there, to make sure the while loop runs at least once (in this case, it printed Y only one time). Another way to put it is: if the statement was true (something like 2 > 1), the while loop would print Y infinite times; without the do, it just wouldn't print Y at all.


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