Lexicographically largest string possible by repeatedly appending first character of two given strings | Function to make the lexicographically largest let by merging two lets ; Stores the resultant let ; If the let word1 is lexographically greater than or equal to word ; Update the let merge ; Erase the first index of the let word1 ; Otherwise ; Update the let merge ; Erase the first index of the let word2 ; Return the final let ; Driver Code
function largestMerge ( word1 , word2 ) { let merge = " " ; while ( word1 . length != 0 word2 . length != 0 ) { if ( word1 . localeCompare ( word2 ) == 0 || ( word1 . localeCompare ( word2 ) > 0 ) ) { merge = merge + word1 [ 0 ] ; word1 = word1 . substring ( 1 ) ; } else { merge = merge + word2 [ 0 ] ; word2 = word2 . substring ( 1 ) ; } } return merge ; } let S1 = " " ; let S2 = " " ; document . write ( largestMerge ( S1 , S2 ) ) ;