Replace every character of string by character whose ASCII value is K times more than it | Function to move string character ; changed string ; iterate for every characters ; ASCII value ; store the duplicate ; if k - th ahead character exceed ' z ' ; print the new string ; Driver Code ; function call
function encode ( s , k ) { let newS = " " ; for ( let i = 0 ; i < s . length ; ++ i ) { let val = s [ i ] . charCodeAt ( 0 ) ; let dup = k ; if ( val + k > 122 ) { k -= ( 122 - val ) ; k = k % 26 ; newS += String . fromCharCode ( 96 + k ) ; } else { newS += String . fromCharCode ( val + k ) ; } k = dup ; } document . write ( newS ) ; } let str = " " ; let k = 28 ; encode ( str , k ) ;