हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना आवश्यक है जो वर्णों की एक स्ट्रिंग लेता है। फ़ंक्शन को एक नई स्ट्रिंग का निर्माण करना चाहिए जिसमें मूल स्ट्रिंग से सभी गैर-वर्णमाला वर्ण हटा दिए जाते हैं और उस स्ट्रिंग को वापस कर देते हैं। यदि स्ट्रिंग में रिक्त स्थान हैं, तो उसे हटाया नहीं जाना चाहिए।
उदाहरण के लिए -
यदि इनपुट स्ट्रिंग है -
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