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


#1

var dangleDonuts = 150;
var getToDaChoppa = function(){
  do {
      console.log("There are" + dangleDonuts + "in two hours and thirty minutes.");
  while (dangleDonuts);
  dangleDonuts++;
};
}
getToDaChoppa();

What do I need here?


#2

Your do-while syntax is incorrect. Proper syntax:

do {
//Statements;
//Statements;
//Incrementation;
} while (CONDITION);

#3

In addition to @cadecodes remark: The condition doesn't make much sense: What do you expect 150 to do and how do you expect this to change its behavior after adding 1 to it?


#4

Can I get some assistance please? I think my syntax is correct but it creates an infinite loop crashing my browser.
My thought process:
1. created a condition outside of the loop
2. Used proper do syntax, I think my while loop is correct...)
Why the infinite loop?

My code:
var condition=1;

var getToDaChoppa = function(){

do {
console.log("I'm tired of looping for a while.");
} while(condition);
count++;
};

getToDaChoppa();


Infinite Loop! Can someone take a look at my "To learn it, you gotta do it"
#5

Hi the error come from you do/ while because you should put the inscrease inside the do/ while instead of after the while,
Here how your do /while should be .

do { 
   // do Something 

  // the increase go here 
}  while(condition)

#6

How do you expect your condition to end? It will be a number in the condition, which is always true.

You should make a comparison to another number to tell it when to stop.


#7

a variable (number) is not needed for this exercise. Take a look at the exercise before this one (lesson 9) and make a comparison to the loop you did in this one (lesson 10). Here's what I used...

var getToDaChoppa = function() {
    do {
    console.log("Your string of choice");
    
    } while(function = ?) {
  } 
  
};

getToDaChoppa();

Since the function isn't declared false from the start, you can declare it within the while. Go ahead and give it a try...


#8

That looks dangerous to me :worried:

First of all function is a javaScript keyword that should not be misused as a variable and the other thing is that using an assignment (variable = value) at this place is not a good idea (unless you fully know what you're doing there).

The problem is that an assignment has the value you assign e.g.

console.log(question = 42); --> 42

So in this case the statement would always have the value of 42. The next question is: How would a loop handle 42? And the answer can be looked up but might not be common knowledge:
http://www.sitepoint.com/javascript-truthy-falsy/
So 42 would be true but 0 would be false. So instead of using values as conditions, where you and others reading your code have to look up how they behave, better use comparisons like == or === or if you deal with numbers <,>,<=,>= aso. An exception to this rule are the boolean values true and false, there you know, or should know, how they behave so using them is ok but in loops it might be pointless. What do I mean by pointless? Well if you set your condition to be false your loop will simply not start and if you set it to true your loop will never stop. So far there is no exercise that requires infinite loops or explains how to use them (game loop, break keyword) and for the "false"-path well if your loop never runs or only runs once, why bother with a loop at all instead of choosing an if or just not execute it.

I know it is shown in the exercise to explain that a while loop runs once even though the condition is false but as said this is just to show this particular behavior, real loops are meant to loop at least twice to almost infinite times which makes them really powerful and interesting.

Used a few examples for all kind of loops (you've met so far) over here:


#9

Thank you, the way you break it down is super helpful and gives me the context to hopefully move forward.