Sha256: 07ed75142a7d42660b7eaa2195ca0a9b42f444a8b8cec6c5452c368ef454fbb2

Contents?: true

Size: 1.28 KB

Versions: 185

Compression:

Stored size: 1.28 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) => accumulator * inputBase + value, 0);
    return convertFromDecimalToBase(decimalValue, outputBase);
  }
}

Version data entries

185 entries across 185 versions & 1 rubygems

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