I bet there is lots of training data with for(let i=1;i<=s1.length;i++) then using s1[i-1] everywhere.
But I think it should be for(let i=0;i<s1.length;i++) then use s1[i]?