10. To learn it, you gotta 'do' it infinite loop


#1

Ok, i don't understand it. It goes infinite on me. Why? Please help.

var getToDaChoppa = function(i){
// Write your do/while loop here!
do {
console.log("RUUUUUUUUUN! GOOOOOO!");
} while(i < 2) {
console.log("GET TO THE CHOPPAAA!!");
i++;
}
}
getToDaChoppa(1);


#2

a)you should set i before your 'do' operator;
b)and your conditions should be written under 'do', not after 'while'


#3

@cloudrockstar96944,

Please read
the while-loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while

the do-while-loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while

You cannot concatenate the do-while with the the while statement's


#4

i could not figure out how to get your code running either. When I undertook this task my approach was pretty much following the previous lessons but if you manage to resolve it will you post your solution.

Mine is nothing like yours...ironically, the way i attempted this lesson ends up being relevant for the next final lesson of this part of section 4 (almost halfway through JS course ...woohoo) but I digress....

var getToDaChoppa = function(){
  // Write your do/while loop here!
  var count = 10;
  do{
      console.log("Count down in t-minus ...");
      count++;
      
      
  }while( count<1);
  for (var i=10;  i>0; i--){
    console.log(i);}
    
console.log("phew!");
  
};

getToDaChoppa();

\ blimey 'e still remembers the trick to add backticks ` when posting code on da forum...

output:

Count down in t-minus ...
10
9
8
7
6
5
4
3
2
1
phew!


#5

Ok, i think i get it. Thank you, guys.


#6

Wow.. Thanks! I actually thought that the while had to be in the do { }s.


#7

Thanks! Could you help me understand why count<1 in your while condition?


#8

hi,

not very good at explaining things but will try to relay what I have sort of understood so far in my learning...

I recall two ways in which we could utilize the iteration i++ and i--

Because I wanted to show the value of the count variable as it counted downwards after each while loop iteration rather than counted upwards via the console.log(i) it meant setting the condition of the while statement as count <1 on the premise that it prints a number as long as it is less than 1.

outside the do while loop, I had (above the do), set the variable named count to 10

var count = 10;
do{
console.log("Count down in t-minus ...");
count++;

in the for loop I had set the conditions

(start = 10 ; finish = 0 ; decrease by 1;)

so whilst the loop runs and the while condition is met

while( count<1)

it would then print out as per the console.log(i) instructions the value of count each time the loop had been executed ....and continued to do this until it reached zero ....iterates just so as long as, after each execution of the loop, it was less no longer less than 1 as the end condition is set to zero.
( count <1)

sorry if that is not very clear its just the way I have come to understand it...I may be wrong in my own understanding maybe someone more knowledgeable can clarify this much better for you.

Also, when I was trying to comprehend the purpose of increments i++ and i-- I stumbled upon the following code which can be found here: http://www.tutorialspoint.com/python/python_while_loop.htm

count = 0
while count < 5:
print count, " is less than 5"
count = count + 1
else:
print count, " is not less than 5"

(because this is count +1 it counts upwards rather than downwards) but sort of how I got to understand the while count < 1 condition

apparently it is a different programming language to JS but it made sense when I read it and I suppose I tried to incorporate that with what we are learning here. Proper novice or what haha.

Examples
Using do...while

In the following example, the do...while loop iterates at least once and reiterates until i is no longer less than 5.

And have a look at the example from the weblink that @leonhard_wettengmx_n provided as this might explain things better than I have done...

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while

Examples
Using do...while

In the following example, the do...while loop iterates at least once and reiterates until i is no longer less than 5.

var i = 0;
do {
i += 1;
console.log(i);
} while (i < 5);

Hope I have not confused you more!


#9

Ok now I'm confused :smile:
So the basics i++ means i=i+1 which therefore increases the value by 1 for example:

    var i=0;
    i++;
    console.log(i) // new value is 1

i-- on the other hand means i=i-1 so the value is decreased by 1. And in case you want to add or remove (or even divide or multiply) a random number you can use the 4th way (i = i+x, i++, i--) of using i operator = x for example:

var i=0;
var x = 2
i+=x;
console.log(i) // new value of i is 2 (x)

Now for your conditions well they should make sense meaning that they allow your loop to run a few times but on the other hand stop after some time. So as the condition is normally countingVariable < (or >) endValue you need to make sure that endValue is bigger than the startValue vise versa for couting down. And this:

(start = 10 ; finish = 0 ; decrease by 1;)
finish = 0

is a pretty dangerous condition because it is not a comparison but an assignment or is the meant as pseudocode? Anyway even then it would not be a good condition for a loop because it could only run never, once and infinite times and therefore an if would be better. Could you give some more examples on what you want to do and what you think you're doing?


#10

confused?...you will be....welcome to my world haha

I am still only part way through my first codecademy programming language course. (though, upon completing intro to command line earlier today, I opted to sign up to learn intro to Java and before dinner I completed the first section and plan to finish section 2 later on) ...be reasonable guv'na I only joined up a few weeks ago

I joined here after being sign posted from the WC3 edX HTML5 part 1 course showed the site in the resources section and I thought I would have a go at learning JS before HTML part 2 commenced this week.... but anyway I digress...

Anyhoo... the question we were given was for this task was not actually requiring a for loop - that just happened to come into play in the next task. It just so happened the way I wrote my code for this task resulted in me adding in the for loop one step earlier than needed.

Instructions:
Write a do/while loop inside the function we've created for you, getToDaChoppa. The function should log a string of your choice to the console. do it now!

The code I wrote was as follows:

var getToDaChoppa = function(){
  // Write your do/while loop here!
  var count = 10;
  do{
      console.log("Count down in t-minus ...");
      count++;
      
      
  }while( count<1);
  for (var i=10;  i>0; i--){
    console.log(i);}
    
console.log("phew!");
  
};

getToDaChoppa();

yep, I think I was using what you call pseudo code to clarify what I was entering into the for loop:

for (start = 10 ; finish = 0 ; decrease by 1;)
for (var i=10; i>0; i--)

I based this on the first lesson of this exercise which showed us the following format:

for (var i = start; i < end; i++) {
// do something
}

@nahahana was asking if I could perhaps indicate why in my code I had written count <1 and as you read I sort of rambled on the approach I had adopted had been based on the exercises that had been covered up to this point in JS plus a couple of signposted places that also show this format when writing a do while loop.

Like I mentioned, rather than have a count that went upwards from 1 to 10, I wanted it to count down from the number starting at 10 and end at 1. the aim was to show a count down through use of console.log(i) and then after exiting the do while loop print a final message...Phew!!! as though the user just made it to the choppa (I know its not a rocket but you get the gist right?....No...? U don't...? K....well, erm....anyhoo....

i can't at this point determine whether this code was good bad or down right ugly. What I wanted the code to do seemed to work .. U are more experienced and knowledgeable so your grasp of JS denotes the way I am writing it will need working on - but isn't that something I will develop over time as I gain more insight into the subject? I just considered completing the task as an achievement.

When @nahahana asked me to give feedback as to how I came to use that particular count < 1 format in my while condition I just indicated I could only really say it was from what I had read via the signposted link provided by @leonhard_wettengmx_n which i found helpful and, having followed along with the lessons so far I could make some sense of what was written in the example it provided. The other example which also seemed to show this format turned out to be a programming language known as python that was located when I entered a quick search in the tutorials website search section.... http://www.tutorialspoint.com/python/python_while_loop.htm

I liked how you gave the example code:

var i=0;
var x = 2
i+=x;
console.log(i) // new value of i is 2 (x)

if I was to amend this to reduce by 2 after each iteration then I imagine the variation would be:

var i=10;
var x = 2
i -= x;
console.log(i) // new value of i is 2 (x)

I would then guess the output would be along the lines:
10, 8, 6, 4, 2

The code I wrote incorporates a while condition (count <1)

}while( count<1);

I draw on the example and explanation given in the following article provided by @leonhard_wettengmx_n

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while

'The while loop iterates at least once and reiterates until i is no longer less than 5.'

in this instance, for my code, I changed the condition so it would iterate until i is no longer less than 1 instead

while (count <1) // condition

still probably not making sense so on that note well... erm...pass.. haha

k. betta get back to finishing the section of java I had started earlier.

again, sorry if what I wrote is confusing.


#11

Ok thanks for the code but what is your actual problem or question? If this is what your for loops looks like then it should be ok and serves its purpose of counting form 10 to 1 does it?
The do while loop works as well although it might be easier to set your condition to false:

  do{
      console.log("Count down in t-minus ...");
  }while(false);

Here are some examples on when to really use a do/while loop:

And yes this is well anticipated only the comment needed to be changed as well.

var i=10;
var x = 2
i -= x;
console.log(i) // new value of i is 8 (x)

#12

k. thnx I did not think about changing this to while (false) instead of the way i wrote it. I just tried something that worked for me.

There is NO question, I was initially responding to @cloudrockstar96944 post and indicated my approach to solving the task set in the lesson was different to theirs. And, in turn, @nahahana then posted asking me why I had incorporated count <1 into the while code I had written. Then, I replied and indicated I was building on what I was learning from this lesson plus a couple of sign posts one of which was the do / while loop @leonhard_wettengmx_n

You are clearly experienced in programming but when newcomers like us are not we post on here thinking its ok to ask probably because what makes sense to one person may not always make sense to another....And, I did mention in my reply to @nahahana I was not very good at explaining things but will try to relay what I have sort of understood so far in my learning...

can you ease up on this a bit? I have stipulated I have only been on codecademy less than four weeks and so its all very new... some of us newcomers really are starting from scratch....I was sign posted to this site from edX and took the forum to be similar in format to their educational forum. Like I said when I was responding to @nahahana I could only explain what I sort of understood they way I had come to understand this do while loop which, in terms of skill and knowledge base just as I have tried to emphasise is almost nil. There will be people like yourselves that can shed light on the subject but sometimes you may forget we are not yet all used to the jargon and so get things mixed up ...we are afterall only human so are bound to make lots of mistakes as we start out on our learning to code journey...it really is no biggie really.


#13

Ok then I got this wrong, sry. If you have question about code or jargon feel free to ask.


#14

thnx. Its early days for me so I will probably be posting often have also started the Java lessons in addition to this JS lesson. I am finding each lesson to be intriguing....lots and lots and endless mistakes though haha.