Find the next number by adding natural numbers in order on alternating indices from last | Function to generate the resultant number using the given criteria ; Storing end result ; Find the length of numeric string ; Traverse the string ; Storing digit at ith position ; Checking that the number would be added or not ; Logic for summing the digits if the digit is greater than 10 ; Storing the result ; Returning the result ; Driver Code
function generateNumber ( number ) { let temp = 0 , adding_number = 0 ; let result = " " ; let len = number . length ; for ( let i = len - 1 ; i >= 0 ; i -- ) { let digit = parseInt ( number [ i ] ) ; if ( temp % 2 == 0 ) { adding_number += 1 ; digit += adding_number ; if ( digit >= 10 ) { digit %= 9 ; if ( digit == 0 ) digit = 9 ; } } result = ( digit ) . toString ( ) + result ; temp += 1 ; } return result ; } let S = " " ; document . write ( generateNumber ( S ) ) ;