My current approach involves generating a single letter, but my code breaks if there is a large amount of data and it exceeds column Z
.
Here is the working code that will produce a, d
:
const countData = [1, 2, 3, 4].length;
const initialLetter = 'A'.toLowerCase();
const initialLetterNumber = initialLetter.charCodeAt(0) - 97;
const lastLetter = String.fromCharCode(
initialLetterNumber + countData - 1 + 97
);
console.log(initialLetter, lastLetter);
In contrast, here is the non-working code for which I aim to get z, ac
.
const countData = [1, 2, 3, 4].length;
const initialLetter = 'Z'.toLowerCase();
const initialLetterNumber = initialLetter.charCodeAt(0) - 97;
const lastLetter = String.fromCharCode(
initialLetterNumber + countData - 1 + 97
);
console.log(initialLetter, lastLetter);
Below are some example inputs and their corresponding expected outputs:
input: a, data length: 1 output: a,
input: a, data length: 2 output: b,
input: a, data length: 3 output: c,
input: a, data length: 4 output: d,
input: a, data length: 5 output: e,
input: z, data length: 1 output: z,
input: z, data length: 2 output: aa,
input: z, data length: 3 output: ab,
input: z, data length: 4 output: ac,
input: z, data length: 5 output: ad,
input: ad, data length: 1 output: ad,
input: ad, data length: 2 output: ae,
input: ad, data length: 3 output: af,
input: ad, data length: 4 output: ag,
input: ad, data length: 5 output: ah,
input: az, data length: 1 output: az,
input: az, data length: 2 output: ba,
input: az, data length: 3 output: bb,
input: az, data length: 4 output: bc,
input: az, data length: 5 output: bd,
input: bz, data length: 1 output: bz,
input: bz, data length: 2 output: ca,
input: bz, data length: 3 output: cb,
input: bz, data length: 4 output: cc,
input: bz, data length: 5 output: cd,
...