To learn it, you gotta 'do' it


#1

var getToDaChoppa = function(phase){
  // Write your do/while loop here!
  var count = 1;
  do {
      console.log("I am learning JS!");
      i++
  } while(i < 3);
};


getToDaChoppa();

I do not understand how to do the job

The problem with a loop while

Help me please


#2

Where did you get your i ? Instead of using i, used the variable count that you declared above.


#3

I was inattentive, You right, thanks!


#4

I have studied the details and found this option:

var getToDaChoppa = function(phase){
// Write your do/while loop here!
var i = false;
do {
console.log("I am learning JS!");
} while(i);
};

getToDaChoppa();


#5

Yep, that works. Simply because, as you declared your variable i equal to false, and your condition(which is while(i)) is false, the loop will stop. in do/while loop, the loop will stop if the condition is false.

REMEMBER:
The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested


#6

Sample:

var i= 1;
    do {
       console.log("I am learning JS!");
        i++
       } 
    while(i < 3);
};

I think this is the simplest explanation:

I am learning JS! //first loop
1+1 // equal 2 (1 plus 1 = 2) i++
2<3 //TRUE (is 2 greater than 3?) this is your condition your condition is TRUE so keep on looping
I am learning JS! //second loop
2+1 //equal 3 (2 plus 1 = 3) i++
3<3 //FALSE (is 3 greater than 3?) this is now your condition your condition is False so the looping will stop

Thats why it printed "I am learning JS!" twice.


#7

Thanks for the explanation! :slight_smile:


#8

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