通过递归JavaScript构造字符串
我们需要编写一个递归函数,例如pickString,它接受一个包含字母和数字的组合的字符串,并返回一个仅包含字母的新字符串。
例如,
If the string is ‘dis122344as65t34er’, The output will be: ‘disaster’
因此,让我们为该递归函数编写代码-
示例
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'));输出结果
控制台中的输出将为-
example can you do it hello this is the last example
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短