当前位置:网站首页>字符串加千分位符与递归数组求和
字符串加千分位符与递归数组求和
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
边栏推荐
猜你喜欢

Classes and Objects (Part 2)

ISELED---the new choice of ambient lighting scheme

How to implement timing tasks for distributed applications in Golang

How to intercept the first few digits of a string in php

应用接入华为分析在应用调试模式下为何没有数据上报?

一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?

70 lines of code, a desktop automatic translation artifact

【重磅来袭】教你如何在RGBD三维重建中获取高质量模型纹理

动态规划 --- 状态压缩DP 详细解释

华为「天才少年」计划招募的博士们,迎来首秀!
随机推荐
tiup completion
【HMS core】【FAQ】Account、IAP、Location Kit and HarmonyOS典型问题合集1
RISC-V calling conventions
HTTP缓存小结
【AGC】Open Test Example
PMP每日一练 | 考试不迷路-7.30(包含敏捷+多选)
tiup list
Xshell命令
L2-007 家庭房产(vector、set、map的使用)
yarn的安装及使用教程
Redis 复习计划 - Redis 数据结构和持久化机制
[Cloud Native] Service Industry Case - Solutions for Unpredictable Concurrency Scenarios
武汉星起航:海外仓基础建设成为跨境电商企业的一大出海利器
一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?
php如何查询字符串出现位置
When the vite multi-page application refreshes the page, it will not be in the current route and will return to the root route
TiUP FAQ
Nature Microbiology综述:聚焦藻际--浮游植物和细菌互作的生态界面
影像信息提取DEM
Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer