0

Say I want to build an array of numbers base 8, or base 26, I'm not sure how to approach a general formula for doing this:

console.log(arrayOfNumbersOfBase(8, 0, 10));
console.log(arrayOfNumbersOfBase(26, 0, 10));

function arrayOfNumbersOfBase(base, start, size)
{
    var array = [];
  
    for (var i = start, n = size; i < n; i++)
    {
        array.push(i * (base));
    }

    return array;
}
Shidersz
  • 16,846
  • 2
  • 23
  • 48
Lance
  • 75,200
  • 93
  • 289
  • 503
  • Can you post an example output that you're looking for from one of those calls? – CertainPerformance Jan 15 '19 at 19:57
  • No sorry, I can only do it for base 10 => 0,1,2,3,4,5,6,.... Maybe base 2: 0,1,10,11,... – Lance Jan 15 '19 at 19:57
  • If you don't know the sort of output you're expecting, it will be hard to figure out the logic needed to construct the desired output... – CertainPerformance Jan 15 '19 at 19:58
  • The logic is "base_n number list". But I don't know how to construct it so I don't know the desired output. – Lance Jan 15 '19 at 19:59
  • Lance, is the `array` supposed to have a string representation of the number or just a number? Thing is that a number is the same irrespective of the base so your examples of base 10 => 0,1,2,3,... and of base 2 => 0,1,10,11 are the same. 2dec = 10bin and 3dec = 11bin. It is just a list of natural numbers and the base does not affect how it is built. So what exactly do you want? To put it otherwise, assume `arrayOfNumbersOfBase`, how would you use its result? – SergGr Jan 15 '19 at 22:17

1 Answers1

1

You can take the next approach as a starting point, basically I had to define some utility methods:

  • mapToChar(n) maps a number n to a character representation, for example, 10 is mapped to 'A'.

  • convertToBaseN(n, base) converts the number n to his representation on the given base. This method uses a recursive approach and utilizes the previous one.

  • Finally, generateNumbersOfBase(base, start, size) generates an array of size elements starting with the number start for the given base.

CODE:

// Next utility method map a decimal number to a character representation.

const mapToChar = (n) =>
{
    n = (n >= 0 && n <= 9) ? '0'.charCodeAt() + n : n - 10 + 'A'.charCodeAt();
    return String.fromCharCode(n);
}

// Next utility method convert a decimal number to his base-n representation.

const convertToBaseN = (n, base, res = "") =>
{
    if (n <= 0)
       return (res && res.split("").reverse().join("")) || "0";

    // Convert input number to given base by repeatedly 
    // dividing it by base and taking remainder.

    res += mapToChar(n % base);
    return convertToBaseN(Math.floor(n / base), base, res);
}

// Next method generates an array of numbers for a given base.

const generateNumbersOfBase = (base, start, size) =>
{
    return Array(size).fill(0).map((x, idx) => convertToBaseN(start + idx, base));
}

// Finally, generate some arrays.

let base10Array = generateNumbersOfBase(10, 15, 5);
let base2Array = generateNumbersOfBase(2, 5, 9);
let base16Array = generateNumbersOfBase(16, 10, 12);
let base8Array = generateNumbersOfBase(8, 1, 12);

console.log(
    JSON.stringify(base10Array),
    JSON.stringify(base2Array),
    JSON.stringify(base16Array),
    JSON.stringify(base8Array),
);

Now, if you need to convert some base-n representation back to decimal number, you can use next approach:

const convertToDec = (str, base) =>
{
    let codeA = 'A'.charCodeAt();
    let code0 = '0'.charCodeAt();

    return str.split("").reverse().reduce((acc, c, idx) =>
    {
        let code = c.charCodeAt();
        c = code + ((c >= '0' && c <= '9') ? -code0 : -codeA + 10);
        return acc += c * Math.pow(base, idx);
    }, 0);
}

// Lets convert back some arrays generated on the previous exampel

let base2Array = ["101","110","111","1000","1001","1010","1011","1100","1101"];
let base16Array = ["A","B","C","D","E","F","10","11","12","13","14","15"];

let res2 = base2Array.map(x => convertToDec(x, 2));
let res16 = base16Array.map(x => convertToDec(x, 16));

console.log(
    JSON.stringify(res2),
    JSON.stringify(res16)
);
Shidersz
  • 16,846
  • 2
  • 23
  • 48