Handling the printing of letters from A-H

I have the following JSFiddle which is printing letters from A to H.

const alphabet = Array.from({length: 8}, (x, i) => String.fromCharCode(65 + i));

function printEach(arr) {
  if (arr.length === 0) return; // If arr is empty, return
  console.log(arr.shift());     // Remove the first letter from arr and print it
  return printEach(arr);        // Deal with the remaining elements
}

printEach(alphabet);

If I want to repeat printing letters from A-H starting . I have a loop going on in my code (not shown anywhere in this post)which is making an index available to me from 1-96 and

for the value 0, I want to print A,
for the value 1, I want to print B,
for the value 2, I want to print C,
for the value 3, I want to print D,
for the value 4, I want to print E,
for the value 5, I want to print F,
for the value 6, I want to print G,
for the value 7, I want to print H,
for the value 8, I want to print A,
for the value 9, I want to print B,

so on and so forth …

for the value 95, I want to print H (I believe H will be the 96th value but I may be wrong).

I am wondering if it’s possible to modify my function in such a way I can handle above scenario?

Would the formula just be (65 + (loopval % 8)) ?

2 Likes
for(const i of Array(length).keys()) { console.log(String.fromCharCode(65 + (i % 8))); }

So your code would go inside a loop running from 0 to 95 ?

Thanks. Following code seems to be working:

for (i = 0 ; i < 96 ; i++){

 console.log(String.fromCharCode(65 + (i % 8)));

}

Here’s a link for you Remainder :slight_smile: