Largest number with prime digits | check if character is prime ; replace with previous prime character ; if 2 erase s [ i ] and replace next with 7 ; find first non prime char ; find first char greater than 2 ; like 20 ; like 7721 ; replace remaining with 7 ; Driver code
function isPrime ( c ) { return ( c == ' ' c == ' ' c == ' ' c == ' ' ) ; } function decrease ( s , i ) { if ( s [ i ] <= ' ' ) { s . splice ( i , 1 ) s [ i ] = ' ' ; } else if ( s [ i ] == ' ' ) s [ i ] = ' ' ; else if ( s [ i ] <= ' ' ) s [ i ] = ' ' ; else if ( s [ i ] <= ' ' ) s [ i ] = ' ' ; else s [ i ] = ' ' ; return s ; } function primeDigits ( s ) { for ( let i = 0 ; i < s . length ; i ++ ) { if ( ! isPrime ( s [ i ] ) ) { while ( i >= 0 && s [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) i -- ; if ( i < 0 ) { i = 0 ; s = decrease ( s . split ( " " ) , i ) ; } else s = decrease ( s . split ( " " ) , i ) ; for ( let j = i + 1 ; j < s . length ; j ++ ) s [ j ] = ' ' ; break ; } } return s . join ( " " ) ; } let s = " " ; document . write ( primeDigits ( s ) + " " ) ; s = " " ; document . write ( primeDigits ( s ) + " " ) ; s = " " ; document . write ( primeDigits ( s ) + " " ) ; s = " " ; document . write ( primeDigits ( s ) + " " ) ; s = " " ; document . write ( primeDigits ( s ) + " " ) ;