Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | JavaScript program to solve the above approach ; initializing first and second variable as to store how many string ' s ' and string ' t ' will be appended ; storing tmp length ; if length of string tmp is greater than k , then we have reached our destination string now we can return character at index k ; appending s to tmp , f times ; appending t to tmp , s times ; extracting output character ; Driver code
function KthCharacter ( s , t , k ) { let f = 1 ; let ss = 2 ; let tmp = " " ; let len = tmp . length ; while ( len < k ) { let tf = f ; let ts = ss ; while ( tf -- != 0 ) { tmp += s ; } while ( ts -- != 0 ) { tmp += t ; } f += 2 ; ss += 2 ; len = tmp . length ; } let output = tmp [ k - 1 ] ; return output ; } let S1 = " " , S2 = " " ; let k = 4 ; let ans = KthCharacter ( S1 , S2 , k ) ; document . write ( ans ) ;