To learn it, you gotta 'do' it


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-ASGIv/2/3?curriculum_id=506324b3a7dffd00020bf661

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!
  do{
      console.log ("X");
  }
  while (1 > 2)
  {
      console.log ("Y");
  }
};

getToDaChoppa(6);<img src="//codecademy-discourse.s3.amazonaws.com/original/5X/d/2/2/a/d22a1b5e4fa7ab43fd3778d0ebc55d5b05c4c037.png" width="690" height="370">


#2

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.


#3

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


#4

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


#5

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


#6

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 repl.it link; I didn't know it until now).


#7

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.


#8

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