Why does this while loop not work? (infinite)

0

I'm having trouble with a while loop in my code. For some reason, it is looping endlessly. The code is this:

function transf(){

var container = document.getElementById('container');
var compartimento = document.getElementById('compartimento');  

while (compartimento.childNodes.length > 0) {
container.appendChild(compartimento.childNodes[0]);
}  

var alturadocontainer = container.clientHeight;  
var containerlc = container.lastChild;    
var container2 = document.getElementById("container2");

while (alturadocontainer > 100){ 
container2.insertBefore(containerlc, container2.childNodes[0]);    
}

}

The first while loop works without problems. The problem is in the second. The expected result is that as long as the height of the container is greater than 100, the last elements of the container pass to container2 (which, in theory, should decrease the height of the container, but apparently this is not happening, infinite loop). Why is this happening?

    
asked by anonymous 14.02.2018 / 14:06

1 answer

0

See the adjustment, updating the value of the variables after the transfer: I did not get to test the code.

function transf(){

var container = document.getElementById('container');
var compartimento = document.getElementById('compartimento');  

while (compartimento.childNodes.length > 0) {
container.appendChild(compartimento.childNodes[0]);
}  

var alturadocontainer = container.clientHeight;  
var containerlc = container.lastChild;    
var container2 = document.getElementById("container2");

while (alturadocontainer > 100){ 
container2.insertBefore(containerlc, container2.childNodes[0]);    
var alturadocontainer = container.clientHeight;  
var containerlc = container.lastChild; 
}

}
    
14.02.2018 / 14:12