Lesson 10: Is there an infinite loop somewhere that I'm not spotting?


#1

Hi, so I wrote my code as displayed below and it keeps causing my browser to crash. I assume this is because I accidentally created an infinite loop. I'm under the impression that a loop is finite if the variable within the while loop is changed to false? (i.e myMamaDontLikeYou = false within the while loop) But I guess this isn't so?

Anyway, I was hoping to end up with 2 sentences on my console. The first one "Is it too late now to say sorry?" from the do loop and the second from running the while loop once.

Thanks in advance for any help/ advice! :slightly_smiling:


#2

While you did set initial value true to you variable, you didn't send that variable to the function. It is not visible there, that's why your code is crashing.
My suggestion is that you declare variable that will be your while condition inside the function.
Also, this should run only once I believe so you could write it like this

var getToDaChoppa = function(){
  // Write your do/while loop here!
  var loopCond = false;
  do {
      console.log("Get down! I said get down! Get down again!");
  } while(loopCond);
};

getToDaChoppa();

Otherwise, you will run it once (if you pass true as condition the first time), then change value to false, run it again then check and only after second run the loop will stop.


#3

Hey @igorcaletacar, thanks so much! I declared my variable inside the function like you said and defined it as false and it produced the outcome I wanted!

Thanks again!
anthearosechy


#4

Is it possible to get some clarification on this solution? As I understand it, the code is saying, "hey, print out this string of words while loopCond is true" but loopCond is set initially to false. If the var is defined as false from the start, how does it run the "while true" part?

Hope the question makes sense, thanks for your help!


#5

Sure, I get what you are asking and I will try to clarify and make it as simple as possible.

So to make the long story short, here is your question:

And the answer is pretty simple. I posted solution with do while loop, and while while loop wouldn't run in this case, do while loop always runs at least once before checking the condition. Check this picture of do while logic to maybe better understand what I meant.

So to sum it, because there is not much more to say about this theme:

  • you are right, while loop with initial condition set to false would never run
  • since we used do while loop, it always runs at least once, and only checks conditions after each run

I hope it's a bit clearer for you now, but if you need more explanations/examples, let me know and I'll do my best to try and help.


#6

I totally get it now, thank you!!


#7

Why do they have a function setup if it isn't used at all?


#8

How it isn't used? We are calling that function on the last line.


#9

I'm new to JavaScript and to be honest, I'm struggling so please don't think I'm questioning you, I'm trying to understand better. I don't see how it is being called. Also, I ran this in the code lab and it works fine. I commented out the function variable as well as the call at the bottom. It works just fine this way, which is why 1) I don't understand how it is called, and 2) why it is used at all. I'm being forced into a roll at work where JavaScripting will be about 80% of the work, any clarification would be greatly appreciated.

//var getToDaChoppa = function(){
// Write your do/while loop here!
var loopCond = false;
do {
console.log("Get down! I said get down! Get down again!");
} while(loopCond);
//};

//getToDaChoppa();


#10

Hi there, to clarify again your question:

When you comment out this line

and this

It becomes a while condition which will run in a code lab just fine like you saw.
First to understand better you have to realize that the do conditon will loop over and across a given set of conditions given a set of indices. Think of it like if else statements with return conditions. I hope this helps.