Sha256: a9ca4778604fee13feb79930f717ff83aa26a4e20fea323002ece38c554f3f64
Contents?: true
Size: 555 Bytes
Versions: 185
Compression:
Stored size: 555 Bytes
Contents
const BASE = 3; export default class Trinary { constructor(decimal) { this.digits = [...decimal].reverse().map(Number); } toDecimal() { if (this.someDigitIsInvalid()) { return 0; } return this.digits.reduce(this.accumulator, 0); } someDigitIsInvalid() { const greaterThanBase = this.digits.some(d => d >= BASE); const notANumber = this.digits.some(d => isNaN(d)); return greaterThanBase || notANumber; } accumulator(decimal, digit, index) { return decimal += digit * Math.pow(BASE, index); } }
Version data entries
185 entries across 185 versions & 1 rubygems