Balanced Ternary Number System | Javascript program to convert positive decimals into balanced ternary system ; Driver code
function balancedTernary ( n ) { var output = " " ; while ( n > 0 ) { var rem = n % 3 ; n = parseInt ( n / 3 ) ; if ( rem == 2 ) { rem = - 1 ; n ++ ; } output = ( rem == 0 ? ' ' : ( rem == 1 ) ? ' ' : ' ' ) + output ; } return output ; } var n = 238 ; document . write ( " " + n + " " + balancedTernary ( n ) ) ;