우리는 문자열을 받아 공백을 제거하고 소문자로 변환하고 영어 알파벳의 해당 문자 위치를 설명하는 숫자 배열을 반환하는 함수를 작성해야 합니다. 문자열 내의 공백이나 특수 문자는 무시해야 합니다.피>
예를 들어 -
Input → ‘Hello world!’ Output → [8, 5, 12, 12, 15, 23, 15, 18, 12, 4]
이에 대한 코드는 -
예시
const str = 'Hello world!';
const mapString = (str) => {
const mappedArray = [];
str
.trim()
.toLowerCase()
.split("")
.forEach(char => {
const ascii = char.charCodeAt();
if(ascii >= 97 && ascii <= 122){
mappedArray.push(ascii - 96);
};
});
return mappedArray;
};
console.log(mapString(str)); 출력
콘솔의 출력은 -
[ 8, 5, 12, 12, 15, 23, 15, 18, 12, 4 ]