문자열을 받아 문자열의 모든 문자를 영어 알파벳에서 다음 요소로 변경하는 JavaScript 함수를 작성해야 합니다.
예:문자열이 -
인 경우const str = 'how are you';
그러면 출력은 다음과 같아야 합니다. -
const output = 'ipx bsf zpv'
예시
다음은 코드입니다 -
const str = 'how are you';
const isAlpha = code => (code >= 65 && code <= 90) || (code >= 97 && code
<= 122);
const isLast = code => code === 90 || code === 122;
const nextLetterString = str => {
const strArr = str.split('');
return strArr.reduce((acc, val) => {
const code = val.charCodeAt(0);
if(!isAlpha(code)){
return acc+val;
};
if(isLast(code)){
return acc+String.fromCharCode(code-25);
};
return acc+String.fromCharCode(code+1);
}, '');
};
console.log(nextLetterString(str)); 출력
다음은 콘솔의 출력입니다 -
ipx bsf zpv