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

Version Path
trackler-2.2.1.180 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.179 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.178 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.177 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.176 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.175 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.174 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.173 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.172 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.171 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.170 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.169 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.167 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.166 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.165 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.164 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.163 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.162 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.161 tracks/ecmascript/exercises/trinary/example.js
trackler-2.2.1.160 tracks/ecmascript/exercises/trinary/example.js