Iterate till N
while ( n > 0 ) { let cur = str [ 0 ] . charCodeAt ( ) ; let ind = 0 ; for ( let j = 1 ; j < n ; j ++ ) { if ( str [ j ] . charCodeAt ( ) < cur ) { cur = str [ j ] . charCodeAt ( ) ; ind = j ; } }