第407天 手写一个trim()的方法 作者:cxwht [3+1官网](http://www.h-camel.com/index.html) [我也要出题](http://www.h-camel.com/contribution.html)
Activity
wheatup commentedon May 27, 2020
forever-z-133 commentedon May 28, 2020
你猜,和两个 for 相比哪个更快些。
按我理解的正则回溯的知识来看,这效率对比还真说不准。
bozaigao commentedon Sep 22, 2020
function trim(str) {
if (str[0] === ' ' && str[str.length - 1] === ' ') {
return trim(str.substring(1, str.length - 1))
} else if (str[0] !== ' ' && str[str.length - 1] === ' ') {
return trim(str.substring(0, str.length - 1))
} else if (str[0] === ' ' && str[str.length - 1] !== ' ') {
return trim(str.substring(1, str.length))
} else if (str[0] !== ' ' && str[str.length - 1] !== ' ') {
return str;
}
}
cool-delete commentedon Sep 22, 2020
@bozaigao
你可能想写尾递归 不过无论是浏览器环境 还是node默认配置 都不会启动尾递归优化哦