1

function translateInWord(mas){
  mas = [].concat(...mas);
  for (let i = 0; i < mas.length; i++){
    mas[i] = String.fromCharCode(mas[i]);
  }
  return mas;
}
console.log(translateInWord([[6,8,13],[5,3,0]]));

I want the program to show me every letter in ASCII from the array 'mas` (that is, it should turn out ['G','I','N','F','D','A'])

3
  • ASCII code for A is 65, not 0. Perhaps you mean the offset in the English alphabet rather than ASCII? Commented Dec 7, 2021 at 14:53
  • ^ then 97 would also be the offset in the English alphabet, just lowercase. Better get away from ASCII at all and redefine this problem. The offsets relative to a specific const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Commented Dec 7, 2021 at 14:59
  • @Thomas I didn't mean any computer encoding. The English alphabet only has 26 letters, with uppercase/lowercase not being considered different letters, so 97 would be off limits. ASCII has seemingly become a generic idiom for any numeric encoding of text. Commented Dec 8, 2021 at 12:19

1 Answer 1

2

You are using a 0-indexed representation of the alphabet (0 -> A, ..., 25 -> Z).

fromCharCode() is taking UTF-16 code units as parameters.

If you look at an UTF-16 table, the decimal representation of A is 65 and the following 25 code units are corresponding to the other capital letters of the alphabet up to Z.

So you just need to add 65 to mas[i] to get its corresponding letter :

function translateInWord(mas){
  mas = [].concat(...mas);
  for (let i = 0; i < mas.length; i++){
    mas[i] = String.fromCharCode(mas[i] + 65);
  }
  return mas;
}
console.log(translateInWord([[6,8,13],[5,3,0]]));

Sign up to request clarification or add additional context in comments.

2 Comments

You should include some explanation in answers, not just code.
Agreed with @xdumaine, what kind of offset is it? Why it's equal to 65? Answer code is not obvious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.