Why doesn't this script end in infinite loop?
A element can only be child of one other element.
calculator.appendChild(document.body.firstChild);
As soon as you attach document.body.firstChild
to your calculator, it automatically detaches from body, so body will eventually run out of children.