code: function bts2n(s) {
    var n = 0;
    for (var i = 0; i < s.length; ++i) {
      var ch = s.charAt(i);
      var digit = BT_DIGIT_TO_N[ch];
      if (digit === undefined) throw new Error('bts2n('+s+'): invalid digit character: '+ch);
      //console.log(i,digit,3**i,n,s,ch);
      n += pow3(s.length - i - 1) * digit;
    }
    return n;
  }
docs: parse balanced ternary string to signed integer