Sha256: 12f422d4c05ce76ca284cce9623fb09ae6f2e8ced977e5a57395dcee2f512545

Contents?: true

Size: 1.33 KB

Versions: 366

Compression:

Stored size: 1.33 KB

Contents

'use strict';

const Converter = function () { };

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;
};

Converter.prototype.convert = function (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);
};

module.exports = Converter;

Version data entries

366 entries across 366 versions & 1 rubygems

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