You gotta do it lesson


#1


Hi, I am trying to make my do while loop that will count from 5 to 0, and when there is only 1 left, or when it gets to 1, it will print, and while it is equal to 1, it will print the additional message. What am I doing wrong? I am getting a syntax error, and don't think I am really grasping the purpose of this lesson. Thanks for your help.


var getToDaChoppa = function(){
  do {
      var menLeft = 5
      for (var i = menLeft; i < 2; i--) {
      console.log("Get to da CHOPPA!!!");
      while (i = 1) {
          console.log("Time is of the essence!");
      }
      }
};

getToDaChoppa();


#2

Never set a counting variable inside a loop. It will cause an infinite loop under the right circumstances. Always set it before the loop.

var menLeft = 5;
do {

} while (  );

Notice in the example there is no code block after the while condition? The code block is before that. In your code you have a while loop inside the do..while loop, but the do {} loop has no while condition.

As for the code in general, it is chalk full of errors and needs to be completely redone. I suggest go back to the beginning of the unit and read all the lesson text thoroughly. Even do some extra reading so you get a better understanding of while and do..while loops. No point rushing through this and moving forward.

All the more reason to stop right here and do a thorough review of everything that you're studied up to this point. Showing you the solution is not going to help.


#3

It still doesn't work, even after putting the var menLeft above and outside of the do loop. Not sure what you mean about having the while loop inside of the do while loop, and the while of the do while not having a condition. Thanks for your help though.


#4

I think the advice to act on is to start over - copy that code to somewhere else to not discard it completely yet, and then add things back one at a time, for example start with an empty function, call it and see that it runs without errors (but obviously does nothing, is empty), then add console.log, run again, it should now be printing something, then put a loop around it, see that it runs the right amount of times .. and so no - so that each time you add something you can verify that it has the intended effect and otherwise it's probably the last thing you added that caused the mess and you can look up how to do that thing properly

And if you seem to be doing something correctly but adding it messes things up, then do just that thing entirely by itself and see if it's still correct

..so isolate each thing that you're not sure about, get it working, and only then add it to the rest


#5

Maybe the function does not have the capability of having all that I put in it? Or is it a syntax issue? Telling someone to start over is not inappropriate as far as figuring out the issue on one's own, however, as this is a forum for help and advice, telling one to just start over isn't especially helpful.


#6

The point of starting over is to get each component working by itself. Since you're using several incorrectly, what you need to do is to disassemble it and look at each part by itself

So, it's not just "start over", it's "start from scratch and add them back in a way that allows you to find the problems"

If you don't understand each component sufficiently well, then it makes it that much more difficult to reason about each problem. Better reason about them in isolation of each other.


#9

I simplified it, but it still doesn't work:

var getToDaChoppa = function(){
var menLeft = false
do {
console.log("Get to da CHOPPA!!!");
};
while(menLeft = true);

getToDaChoppa();


#10

Compare your loop to some reference/examples - try googling mdn javascript do while

Removing the loop allows you to determine that the loop is the problem, which is therefore the thing you'd need to look up


#11

var menLeft = 3
var getToDaChoppa = function(){
do {
console.log("Get to da CHOPPA!!!");
menLeft -1;
};
while(menLeft > 0);

getToDaChoppa();

I can't see why this one doesn't work.


#12

For starters you have more {'s than you have }'s , this should be very easy to spot if you attempt to keep your indentation correct, which you should do at all times -- you would have code that's "hanging out" too far to the right at the bottom of your code since there's no } to bring it back all the way to the left side

Try running the example on mdn's reference on do-while - that runs, yes? (Just that code) -- then you can compare that example to how you use do-while.
And since you're trying to get your loop to run, do it without the function to interfere so that you only modify the thing you're trying to get working and thereby eliminate any chance of some neighboring code interfering


#13

I did look at that but it uses slightly different code, so I wanted to stick to this first.
EDIT: I added the } I was missing, thanks, but it still doesn't run.

var menLeft = 3
var getToDaChoppa = function(){
do {
console.log("Get to da CHOPPA!!!");
menLeft -1;
};
};
while(menLeft > 0);

getToDaChoppa();


#14

Remove the parts that are different then. You need to find the pattern that makes out a do-while loop, javascript won't accept anything else, because anything else isn't a do-while loop


#15

My most recent seems to have the same pattern, I still can't see what's wrong.


#17

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