通过递归 JavaScript 构造字符串
javascriptweb developmentobject oriented programming更新于 2025/10/8 18:20:17
我们需要编写一个递归函数,例如 pickString,它接受一个包含字母和数字组合的字符串并返回一个仅由字母组成的新字符串。
例如,
如果字符串为 ‘dis122344as65t34er’, 则输出将为:‘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
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

