searching the first character left of index k and not equal to greatest character of the string
for ( var i = k - 1 ; i >= 0 ; i -- ) { if ( s [ i ] !== v [ v . length - 1 ] ) { for ( var j = 0 ; j < i ; j ++ ) { document . write ( s [ j ] ) ; }