Pls help me figure out what's wrong


#1


could someone please hel me with this


ex 11/11 while loops java script


I don't understand what's wrong with my code it it crashes and I don't know why


for (var i = 0; i < 3; i++){
var bool = Math.random();
var win;
if (bool <= 0.5){ win = true;}
else{ win = false;}
do{ console.log("good luck");}while(win)
while(win){
console.log("well done");
var bool = Math.random();
if (bool <= 0.5){ win = true;}
else{ win = false;}
};
};


#2

this:

do{ console.log("good luck");}while(win)

is an infinity loop if bool is lesser or equal to 0.5 because then win is set to true, and it stays true forever in the loop


#3

I don't understant shoulden't it keep repeating it til it geta a false value


#4

then you should update win inside the do while loop:

do {
   console.log("good luck");
   # update win here
} while(win)

your while loop will keep repeating until you have a false, your do while does not, it only logs something to the console


#5

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