Nambiar Number Generator | Function to return the Nambiar number of the given number ; If there is no digit to choose ; Choose the first digit ; Chosen digit 's parity ; To store the sum of the consecutive digits starting from the chosen digit ; While there are digits to choose ; Update the sum ; If the parity differs ; Return the current sum concatenated with the Numbiar number for the rest of the string ; Driver code
function nambiarNumber ( str , i ) { if ( i >= str . length ) return " " ; let firstDigit = ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; let digitParity = firstDigit % 2 ; let sumDigits = 0 ; while ( i < str . length ) { sumDigits += ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; let sumParity = sumDigits % 2 ; if ( digitParity != sumParity ) { break ; } i ++ ; } return ( " " + sumDigits + nambiarNumber ( str , i + 1 ) ) ; } let str = " " ; document . write ( nambiarNumber ( str , 0 ) ) ;