Check if a number has digits in the given Order | Check if the digits follow the correct order ; to store the previous digit ; pointer to tell what type of sequence are we dealing with ; check if we have same digit as the previous digit ; checking the peak point of the number ; check if we have same digit as the previous digit ; check if the digit is greater than the previous one If true , then break from the loop as we are in descending order part ; Driver code
function isCorrectOrder ( n ) { let flag = true ; let prev = - 1 ; let type = - 1 ; while ( n != 0 ) { if ( type === - 1 ) { if ( prev === - 1 ) { prev = n % 10 ; n = Math . floor ( n / 10 ) ; continue ; } if ( prev == n % 10 ) { flag = false ; break ; } if ( prev > n % 10 ) { type = 1 ; prev = n % 10 ; n = Math . floor ( n / 10 ) ; continue ; } prev = n % 10 ; n = Math . floor ( n / 10 ) ; } else { if ( prev == n % 10 ) { flag = false ; break ; } if ( prev < n % 10 ) { flag = false ; break ; } prev = n % 10 ; n = Math . floor ( n / 10 ) ; } } return flag ; } let n = 123454321 ; if ( isCorrectOrder ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;