문자열을 받는 JavaScript 함수를 작성해야 합니다. 함수는 원래 문자열에서 알파벳이 아닌 모든 문자가 제거되고 해당 문자열을 반환하는 새 문자열을 생성해야 합니다. 문자열에 공백이 있으면 제거하면 안 됩니다.
예:
입력 문자열이 -
인 경우const str = 'he@656llo wor?ld';
그런 다음 출력 문자열은 -
여야 합니다.const str = 'he@656llo wor?ld';
예시
다음은 코드입니다 -
const str = 'he@656llo wor?ld';
const isAlphaOrSpace = char => ((char.toLowerCase() !==
char.toUpperCase()) || char === ' ');
const removeSpecials = (str = '') => {
let res = '';
const { length: len } = str;
for(let i = 0; i < len; i++){
const el = str[i];
if(isAlphaOrSpace(el)){
res += el;
};
};
return res;
};
console.log(removeSpecials(str)); 출력
다음은 콘솔의 출력입니다 -
hello world