Aronson 's Sequence | Returns the given number in words ; Get number of digits in given number ; Base cases ; The following arrays contain one digit ( both cardinal and ordinal forms ) , two digit ( < 20 , ordinal forms ) numbers , and multiples ( ordinal forms ) and powers of 10. ; If single digit number ; Here len can be 3 or 4 ; Last two digits ; Handle all powers of 10 ; Handle two digit numbers < 20 ; Function to print the first n terms of Aronson 's sequence ; Check if character is alphabet or not ; Convert number to words in ordinal format and append ; Driver code
function convert_to_words ( num ) { let len = num . length ; if ( len == 0 len > 4 ) { return " " ; } let single_digits_temp = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let single_digits = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let two_digits = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let tens_multiple = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let tens_power = [ " " , " " ] ; let word = " " ; if ( len == 1 ) { word += single_digits [ num [ 0 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; return word ; } let i = 0 , ctr = 0 ; let s = " " ; while ( i < len ) { if ( len >= 3 ) { if ( num [ i ] != ' ' ) { word += single_digits_temp [ num [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] + " " ; word += tens_power [ len - 3 ] + " " ; ctr ++ ; } len -- ; num = num . slice ( 1 , num . length ) ; } else { if ( ctr != 0 ) { s = " " ; word = word . substring ( 0 , word . length - 1 ) ; } if ( num [ i + 1 ] == ' ' ) if ( num [ i ] == ' ' ) word = word + " " ; else word += s + tens_multiple [ num [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; else if ( num [ i ] == ' ' ) word += s + two_digits [ num [ i + 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 1 ] ; else { if ( num [ i ] != ' ' ) word += s + tens_multiple [ num [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] . substring ( 0 , tens_multiple [ num [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] . length - 4 ) + " " ; else word += s ; word += single_digits [ num [ i + 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; } i += 2 ; } if ( i == len ) { if ( word . charAt ( 0 ) == ' ' ) word = word . substring ( 1 , word . length ) ; } } return word ; } function Aronsons_sequence ( n ) { let str = " " ; let ind = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] . toLowerCase ( ) != str [ i ] . toUpperCase ( ) ) { ind += 1 ; } if ( str [ i ] == ' ' str [ i ] == ' ' ) { n -= 1 ; str += convert_to_words ( ( ind ) . toString ( ) . split ( " " ) ) + " " ; document . write ( ind + " " ) ; } if ( n == 0 ) break ; } } let n = 6 ; Aronsons_sequence ( n ) ;