Find the closest and smaller tidy number | Javascript program to find closest tidy number smaller than the given number ; Check whether string violates tidy property ; If string violates tidy property , then decrease the value stored at that index by 1 and replace all the value stored right to that index by 9 ; Driver code ; num will store closest tidy number
function tidyNum ( str1 , len ) { var str = str1 . split ( ' ' ) ; for ( i = len - 2 ; i >= 0 ; i -- ) { if ( str [ i ] > str [ i + 1 ] ) { str [ i ] -- ; for ( j = i + 1 ; j < len ; j ++ ) str [ j ] = ' ' ; } } return str . join ( " " ) ; } var str = " " ; var len = str . length ; document . write ( tidyNum ( str , len ) ) ;