저장을 습관화

자바스크립트 유니코드를 사용하여 알파벳 배열 만들기 본문

공부/JavaScript

자바스크립트 유니코드를 사용하여 알파벳 배열 만들기

ctrs 2023. 9. 29. 17:25

기본 String.fromCharCode() 개념

console.log(String.fromCharCode(65)); // A
console.log(String.fromCharCode(97)); // a
console.log(String.fromCharCode(65, 66, 67)); // ABC

fromCharCode(유니코드 번호)를 입력하면 그와 일치하는 문자가 나오게된다.

유니코드 번호 65부터 90은 대문자 A부터 Z이며

유니코드 번호 97부터 122는 소문자 a부터 z이다.

 

이를 응용하여 아래와 같이 알파벳 배열을 만들 수 있다.

let upperAlphabet = Array(26)
  .fill()
  .map((_, i) => String.fromCharCode(i + 65)); // 유니코드 65 ~ 90

let lowAlphabet = Array(26)
  .fill()
  .map((_, i) => String.fromCharCode(i + 97)); // 유니코드 97 ~ 122

console.log(upperAlphabet);
// [
//   'A', 'B', 'C', 'D', 'E', 'F',
//   'G', 'H', 'I', 'J', 'K', 'L',
//   'M', 'N', 'O', 'P', 'Q', 'R',
//   'S', 'T', 'U', 'V', 'W', 'X',
//   'Y', 'Z'
// ]

console.log(lowAlphabet);
// [
//   'a', 'b', 'c', 'd', 'e', 'f',
//   'g', 'h', 'i', 'j', 'k', 'l',
//   'm', 'n', 'o', 'p', 'q', 'r',
//   's', 't', 'u', 'v', 'w', 'x',
//   'y', 'z'
// ]


console.log([...lowAlphabet, ...upperAlphabet]);

// [
//   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
//   'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
//   'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
//   'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
//   'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
//   'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
//   'W', 'X', 'Y', 'Z'
// ]

 

 

[참조]

https://mine-it-record.tistory.com/489

 

[JavaScript] String.fromCharCode() - 입력받은 유니코드를 해당 유니코드가 의미하는 문자열로 변환하기

유니코드 값을 문자열로 변환시켜주는 String.fromCharCode() 메서드와 그 반대로 특정 문자가 의미하는 유니코드값으로 변환시켜주는 String.prototype.charCodeAt() 메서드에 대해 알아보자. ▷ 구문 String.fr

mine-it-record.tistory.com

https://gurtn.tistory.com/76

 

[JS] a부터 z까지 출력하기

a부터 z까지의 문자를 쉽게 만드는 방법은 유니코드를 사용하여 숫자를 문자로 바꾸는 방법을 사용하는 것입니다. 코드 let str = ''; for (let i = 97; i String.fromCharCode(i + 97)).join(""); 코드 해설 fromCharCod

gurtn.tistory.com

https://programming4myself.tistory.com/98

 

🔠 Javascript로 알파벳 리스트 만들기!

문제 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] 자바스크립트에서 다음과 같이 알파벳으로 이루어진 배열을 만들어보자! 해결 # 소문자

programming4myself.tistory.com