当前位置:网站首页>字符串加千分位符与递归数组求和
字符串加千分位符与递归数组求和
2022-07-30 15:35:00 【废物的自我修养记录】
数组求和
1. 递归
// 只想多看看递归实现
var arr=[1,2,3,4,5]
function addSum(arr){
var sum=0;
if(arr.length===1){
sum=arr[0]
}
else{
sum=arr[0]+addSum(arr.slice(1))
//第一次 sum=1+[2,3,4,5]
//第二次 sum=1+2+[3,4,5]
// ......
}
return sum
}
2.Array.reduce()
function addSum(arr){
return arr.reduce((pre,cur)=>pre+cur,0)
}
字符串返回千分位符
正则实现
function _comma(number) {
return (number+'').replace(/\B(?=(\d{3})+$)/g,',')
}
贴上参照(感谢大佬的文章):
https://www.jianshu.com/p/e143192a544a
边栏推荐
猜你喜欢
随机推荐
深度学习遇到报错Bug解决方法(不定时更新)
HTTP缓存小结
【HMS core】【FAQ】push kit、AR Engine、广告服务、扫描服务典型问题合集2
配置Path环境变量
CAD几个优化设置
tiup help
Google engineer "code completion" tool; "Transformers NLP" accompanying book code; FastAPI development template; PyTorch model acceleration tool; cutting-edge papers | ShowMeAI News Daily
TiUP terms and core concepts
工具| execsnoop 短时进程追踪工具
FME读写cass数据的方案及操作流程
应用OPC解决方案实现控制系统数据的安全交换
Introduction to kasini3000
php如何去除字符串最后一位字符
Flask introductory learning tutorial
Core Topics under Microservice Architecture (2): Design Principles and Core Topics of Microservice Architecture
nodejs环境变量设置
涨姿势了!原来这才是多线程正确实现方式
Flask之路由(app.route)详解
解析字符串拼接的两种情况
ISELED---the new choice of ambient lighting scheme









