Why my code crashes the browser


#1


var getToDaChoppa = function(){
  // Write your do/while loop here!
 var i=5
 do {
     console.log("nah")} 
while (i == 5);
     i = 6;
};

getToDaChoppa();


#2

It doesn't crash, it goes on forever. Your webpage might crash or more likely become unresponsive due to not regaining control.


#3

I though I put a stop to the loop by reassigning the variable? No?


#4

If you change i to something that won't compare as equal to 5, then yes, the loop would stop.


#5

yes but the requirement is to "do at least once" before stopping the loop.

If I change the comparison # 5, the code would not print out even once.


#6

A do-while loop always runs at least once, no matter the result of the condition. That's what makes it different from a while-loop, the condition is checked after each iteration.

Your loop keeps going until you change the value of i.
You change the value of i after the loop.
Your loop doesn't stop, so it never reaches the point where you change i.
Since you don't change i before the loop stops, the loop won't stop, and therefore you don't change i.

i has to be changed before the loop stops. Otherwise it does not stop.


#7

So the point of that lesson is that you're supposed to use a statement that uses a Boolean like true/false. You used a number variable which is fine, but you did it differently than how you would do it if it were a Boolean.

I don't know how to do the code tick marks so this is going to be a bit messy...if someone could tell me how to do those that'd be great! So here goes.

var getToDaChoppa = function(){
// Write your do/while loop here!
var i=5; // In the Boolean version this would be set to false initially. As in var I = false;
do {
console.log("nah");
}
while (i == 5); //In the Boolean version where i=false initially in here would be (i) not (i === true) because i (as a Boolean) has to equal true by default unless otherwise stated, I think. The do while makes it so that it does "nah" at least once and then the point was to cut it off and prevent it from becoming an infinite loop.
i = 6; // <--- this should not be here because it just won't occur ever. Do "nah" while i===5 means that it will never get past the while line because i === 5, so it loops back to do before it can hit the i = 6;.
};

getToDaChoppa();

I'm bad at explaining things like this in a clean way, but your code (and please look through it and compare it) should look like this instead.

var getToDaChoppa = function(){
// Write your do/while loop here!
var i=6;
do {
console.log("nah");
}
while (i===5);
};

getToDaChoppa();

Remove the i=5 after while and switch var i to = 6 in the fitrst place. THis means i already = 6. IT does console.log command once because "do" is there. Then while i===5 (which it doesn't already equal 5) continue to do what's in do.

Alternatively, you can also put the following...

var getToDaChoppa = function(){
// Write your do/while loop here!
var i=5;
do {
console.log("nah");
i = 6; // <--- moved this from after the while loop to the do portion.
}
while (i == 5);
};

getToDaChoppa();

Additionally, you forgot your semicolon after you declared i in the first place so make sure you don't forget those. Not sure why it didn't throw an error right off the bat.

Hope this helped!


#8

Thanks, ionatan. This is really helpful. Could you please also explain why we need this piece of code at the end? (below)

" getToDaChoppa();"


#9

If you don't have that little piece of code in there it won't call the function. Simply making a function doesn't mean it does anything.

Sometimes you'll put stuff in the brackets as well if required of the function if you need to pass in information inside the function.

var newFunction = function(deeze, nutz)
{
console.log("Have you seen " + deeze + " " + nutz + "?");
}

newFunction("some", "value");

// console will print "Have you seen some value?"


#10

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