0

I'm trying to move var letter from A to B to C... and so. I know this is how you would do it in java but how do you do it in javascript?

var letter = 'A';
letter++;


function drawOrganism() {
    var letter = "A"; //Initial Character
    var Organisms = 0;
    for (var col = 0; col <= organismArray.length - 1; col++) {
        for (var row = 0; row <= organismArray[col].length - 1; row++) {
            if (organismArray[col][row] == "*") {
                organismArray = howManyOrganisms(col, row, letter);
                letter++; // Moves up a Character
                Organisms++;
            }
        }
    }
    var printorganism = "";
    for (var i = 0; i <= 2; i++) {
        for (var j = 0; j <= 2; j++) {
            printorganism += organismArray[i][j];
        }
        printorganism += "<br>";
    }
    document.getElementById("drawOrganism1").innerHTML = printorganism;
}
TannerHelms12
  • 43
  • 1
  • 6
  • Please tell what you want? – Ahmed Ali Nov 21 '19 at 08:16
  • 2
    check this - https://stackoverflow.com/questions/12504042/what-is-a-method-that-can-be-used-to-increment-letters – rock11 Nov 21 '19 at 08:17
  • Does this answer your question? [How to get the next letter of the alphabet in Javascript?](https://stackoverflow.com/questions/2256607/how-to-get-the-next-letter-of-the-alphabet-in-javascript) – Cid Nov 21 '19 at 08:22
  • Does this answer your question? [What is a method that can be used to increment letters?](https://stackoverflow.com/questions/12504042/what-is-a-method-that-can-be-used-to-increment-letters) – PJProudhon Nov 21 '19 at 08:22

1 Answers1

1

This may help, You can do this like this.

let letter = 65;
console.log(String.fromCharCode(letter)); // A
console.log(String.fromCharCode(letter + 1)); // B

Also if you want to increment like in a Excel Style you can do it like this:

// 1 -> A, 2 -> B, 26 -> Z, 27 -> AA, 28 -> AB
toExcelXCoordinate(index: number): string {
    let coordinate = '';
    while (index > 0) {
      if (index % 26 === 0) {
        coordinate = 'Z' + coordinate;
        index = (index - 26) / 26;
      } else {
        coordinate = String.fromCharCode((index % 26) + 'A'.charCodeAt(0) - 1) + coordinate;
        index = (index - index % 26) / 26;
      }
    }

    return coordinate;
}

For the Ascii codes, checkout the ASCII table: http://www.asciitable.com/

Ling Vu
  • 4,740
  • 5
  • 24
  • 45