Convert a Mobile Numeric Keypad sequence to equivalent sentence | Function to convert mobile numeric keypad sequence into its equivalent string ; Store the mobile keypad mappings ; Traverse the string str ; If the current character is ' . ' , then continue to the next iteration ; Stores the number of continuous clicks ; Iterate a loop to find the count of same characters ; 2 , 3 , 4 , 5 , 6 and 8 keys will have maximum of 3 letters ; 7 and 9 keys will have maximum of 4 keys ; Handle the end condition ; Check if the current pressed key is 7 or 9 ; Else , the key pressed is either 2 , 3 , 4 , 5 , 6 or 8 ; Driver Code
function printSentence ( S ) { let nums = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let str = S . split ( " " ) ; let i = 0 ; while ( i < str . length ) { if ( str [ i ] == ' ' ) { i ++ ; continue ; } let count = 0 ; while ( i + 1 < str . length && str [ i ] == str [ i + 1 ] ) { if ( count == 2 && ( ( str [ i ] >= ' ' && str [ i ] <= ' ' ) || ( str [ i ] == ' ' ) ) ) break ; else if ( count == 3 && ( str [ i ] == ' ' str [ i ] == ' ' ) ) break ; count ++ ; i ++ ; if ( i == str . length ) break ; } if ( str [ i ] == ' ' str [ i ] == ' ' ) { document . write ( nums [ str [ i ] . charCodeAt ( 0 ) - 48 ] [ count % 4 ] ) ; } else { document . write ( nums [ str [ i ] . charCodeAt ( 0 ) - 48 ] [ count % 3 ] ) ; } i ++ ; } } let str = " " ; printSentence ( str ) ;