Skip to main content
Inspiring
November 15, 2018
Question

Count with A, B, C, D instead of 0, 1, 2, 3, ...

  • November 15, 2018
  • 3 replies
  • 847 views

Essentially, I want to count with letters, like Microsoft Excel does, instead of numbers. So instead of printing "We are at 0", "We are at 1", "We are at 2", etc., I need to print "We are at A", "We are at B", "We are at C", etc...

This topic has been closed for replies.

3 replies

BarlaeDC
Community Expert
Community Expert
November 16, 2018

Hi,

Something like this.

var curLetterAsNumber = 65; // start with a

var bContinue = true;

while ( bContinue){

     var myDisplayString = "We are at " + String.fromCharCode ( curLetterAsNumber);

     console.println ( myDisplayString);

      curLetterAsNumber++;

      if ( curLetterAsNumber > 90)

     {

            bContinue = false;

     }

}

This code just loops and prints the text to the console, but should give you an idea of how to use it.

Regards

Malcolm

hackertomAuthor
Inspiring
November 28, 2018

Because i use greek  Α Β Γ Δ Ε Ζ Η Θ Ι Κ Λ Μ Ν Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω

and not English A B C... i try with ascii table 128 who's is A Greek 129 is B Greek but compiler returns me non character a black cycle dot...

try67
Community Expert
Community Expert
November 28, 2018

Consider using an array or object to associate a number with the corresponding character. For the English alphabet from A-Z it would be:

var aLetters = ["", "A", "B", "C"];  // continue for the other letters

and you'd then do something like:

// Get the letter corresponding to the number 2

var sLetter = aLetters(2);  // Returns "B"

You'd just need to change the single character strings in the array to match what you want to use for each number. With JavaScript, you'd specify the capital Greek A string like this: "\u0391"

So you'd just need to get the unicode values for the other characters and do the same.


The codes are sequential. So "\u0391" (or 913) is indeed ALPHA, then "\u0392" (or 914) is BETA, etc.

hackertomAuthor
Inspiring
November 16, 2018

and how to count++ this?

try67
Community Expert
Community Expert
November 15, 2018

Each character has an ASCII code that you can use to do it. For example, "A" is 65, "B" is 66, etc.

You can convert a character to its code using the charCodeAt method of the String object, and a code to a character using String.fromCharCode(). For example:

"A".charCodeAt(0); //returns 65

String.fromCharCode(65); // returns "A"

So all you have to do is add 65 to your number and then convert it to a character to achieve what you described.

Thom Parker
Community Expert
Community Expert
November 15, 2018

Be sure to test for the limit so you don't go past "Z"

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often