Stepping Numbers | This function checks if an integer n is a Stepping Number ; Initalize prevDigit with - 1 ; Iterate through all digits of n and compare difference between value of previous and current digits ; Get Current digit ; Single digit is consider as a Stepping Number ; Check if absolute difference between prev digit and current digit is 1 ; A brute force approach based function to find all stepping numbers . ; Iterate through all the numbers from [ N , M ] and check if its a stepping number . ; Driver code ; Display Stepping Numbers in the range [ n , m ]
function isStepNum ( n ) { let prevDigit = - 1 ; while ( n > 0 ) { let curDigit = n % 10 ; if ( prevDigit == - 1 ) prevDigit = curDigit ; else { if ( Math . abs ( prevDigit - curDigit ) != 1 ) return false ; } prevDigit = curDigit ; n = parseInt ( n / 10 , 10 ) ; } return true ; } function displaySteppingNumbers ( n , m ) { for ( let i = n ; i <= m ; i ++ ) if ( isStepNum ( i ) ) document . write ( i + " " ) ; } let n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ;