lundi 26 avril 2021

While loop stop after first iteration [closed]

I'm trying to create custom while loop when this happen. The whileloop stop itseft after reaching to if statement. It seems like the condition of the If statement executing itself when the value of x inside the expression match the current value of x. Why does this happen? Is there any other way to do this?

 function test () {
    var x = [0,1,2,3,4,5];
    while (x.length > 0) {

        console.log(x[0]);
        console.log(x.length)

        if (x.length = 0) {
            x = [0,1,2,3,4,5]
           }
        console.log(x[0]);

        console.log(x.length)

        x.splice(0,1)
     } 
}

Aucun commentaire:

Enregistrer un commentaire