Shift Js

Title
Shift


ciao mondo

<!DOCTYPE html>
<!--
alessandro Barazzuol
-->
<html>
    <head>
        <title>Title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script>
            
        /*le strighe sono immutabili in JS, per cui bisogna definire la funzione */   
        String.prototype.replaceAt=function(index, replacement)
        { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } 
       
       
        var k=1; 
        let array;
         
        function shift()
        {
                        if(k%11==0)
                            k=1;
                        /*swap con successivo*/
                        for( j=0;j<(array.length)-1;j++)
                        {
                          
                           var tmp=array.substr(j,1);
                           array=array.replaceAt(j,array.substr(j+1,1));
                           array=array.replaceAt(j+1,tmp);
                          
                        }
                  
                  
                    document.getElementById("t").innerHTML=array;
                    //document.getElementById("t").style.backgroundColor = "red";
                    
                   document.getElementById("b").value=k++;
                   const t=setTimeout(shift,800);
                    
        
   
        }      
            
        window.onload=function()
        {
                 array=document.getElementById("t").textContent;
                 shift();
        }
            

        </script>
    </head>
    <div >
        <div>Shift</div><br>
        <input  id="b"  type="button" onclick="shift();"><br>
        <h1 id="t">ciao mondo</h1>
    </div>
</html>