
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Construct string via recursion JavaScript
We are required to write a recursive function, say pickString that takes in a string that contains a combination of alphabets and numbers and returns a new string consisting of only alphabets.
For example,
If the string is βdis122344as65t34erβ, The output will be: βdisasterβ
Therefore, letβs write the code for this recursive function β
Example
const str = 'ex3454am65p43le'; const pickString = (str, len = 0, res = '') => { if(len < str.length){ const char = parseInt(str[len], 10) ? '' : str[len]; return pickString(str, len+1, res+char); }; return res; }; console.log(pickString(str)); console.log(pickString('23123ca43n y43ou54 6do884 i43t')); console.log(pickString('h432e54l43l65646o')); console.log(pickString('t543h54is 54i5s 54t43he l543as53t 54ex87a455m54p45le'));
Output
The output in the console will be β
example can you do it hello this is the last example
Advertisements