-2

Just wondering about the line after the if statement and why are we subtracting 64 or using 64 in the code for the string.

function alphabetPosition(text) {
  var result = "";
  for (var i = 0; i < text.length; i++) {
    var code = text.toUpperCase().charCodeAt(i)
    if (code > 64 && code < 91) result += (code - 64) + " ";
  }

  return result.slice(0, result.length - 1);
}
console.log(alphabetPosition("The sunset sets at twelve o' clock."));
Otis Guess
  • 17
  • 4
  • 1
    Since the A is 65, subtracting 64 will yield A to 1. And the space after that is just to separate each position – NightEye May 03 '22 at 06:04
  • 2
    65 Is the letter `A`, and 90 is `Z`. So by subtracting 64, you get `1` for A, up to `26` for Z. – CBroe May 03 '22 at 06:07

1 Answers1

2

As an example for explanation: the code of A is 65 but its alphabetical position is 1. So if you need to get the letter’s alphabetical position from its code just subtract 64.