Sha256: 602f16722a81940f4266b51906f1589f83594f998cb655af858b199e3bac5a90

Contents?: true

Size: 1.31 KB

Versions: 169

Compression:

Stored size: 1.31 KB

Contents

const isValidBase = function (base) {
  return !base || base < 2 || Math.floor(base) !== base;
};

const isInputValid = function (array, base) {
  if (!array || !array.length) {
    return false;
  }
  const val = base - 1;
  for (let i = 0, n = array.length; i < n; i++) {
    const tmp = array[i];
    if (tmp > val || tmp < 0) {
      return false;
    }
  }
  return true;
};

const convertFromDecimalToBase = function (num, outputBase) {
  let tmp = num;
  const result = [];
  while (tmp) {
    result.unshift(tmp % outputBase);
    tmp = Math.floor(tmp / outputBase);
  }
  return result;
};

export default class Converter {
  convert(array, inputBase, outputBase) {
    if (isValidBase(inputBase)) {
      throw new Error('Wrong input base');
    }
    if (isValidBase(outputBase)) {
      throw new Error('Wrong output base');
    }
    const regexp = new RegExp('^0.', 'g');
    const str = array.join('');
    if (str.match(regexp)
      || !isInputValid(array, inputBase)) {
      throw new Error('Input has wrong format');
    }
    if (str === '0') {
      return [0];
    }
    if (str === '1') {
      return [1];
    }
    const decimalValue = array
      .reduce((accumulator, value) => {
        return accumulator * inputBase + value;
      }, 0);
    return convertFromDecimalToBase(decimalValue, outputBase);
  }
}

Version data entries

169 entries across 169 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/all-your-base/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/all-your-base/example.js