当前位置:网站首页>字符串加千分位符与递归数组求和
字符串加千分位符与递归数组求和
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
边栏推荐
- php字符串如何去除第一个字符
- Store Limit usage documentation
- (Popular Science) What is Fractional NFT (Fractional NFT)
- 【HMS core】【Media】【Video Editing Service】 The online material cannot be displayed, it is always in the loading state or the network is abnormal
- ECCV2022 | FPN错位对齐,实现高效半监督目标检测 (PseCo)
- STM32F407定时器输入捕获
- 开源WebGIS架构
- 三维重建方法汇总
- 科研中一些常用软件清单
- 五只小猪的案例(五只小猪 比较体重的大小)
猜你喜欢
[Developers must see] [push kit] Collection of typical problems of push service 2
FME读写cass数据的方案及操作流程
Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer
70 lines of code, a desktop automatic translation artifact
(Popular Science) What is Fractional NFT (Fractional NFT)
[HMS core] [FAQ] Collection of typical problems of push kit, AR Engine, advertising service, scanning service 2
Flask introductory learning tutorial
PMP每日一练 | 考试不迷路-7.30(包含敏捷+多选)
How to remove last character from string in php
ISELED---the new choice of ambient lighting scheme
随机推荐
深度学习遇到报错Bug解决方法(不定时更新)
Shell脚本的概念
rscsa笔记八
应用OPC解决方案实现控制系统数据的安全交换
Back waves are coming!Ali produced the "second generation" container technical manual and brain map, which is too fragrant
70行代码撸一个桌面自动翻译神器
Data Analysis Tools - DDL operations & DML operations in HQL
[flutter]什么是MaterialApp和Material design
tiup env
Troubleshooting TiUP
php how to query string occurrence position
【HMS core】【FAQ】push kit、分析服务、视频编辑服务典型问题合集3
转换OTU表和序列文件为PICRUST2需要的格式
Applicable scenarios of TiDB tools
【喂到嘴边了的模块】准备徒手撸GUI?用Arm-2D三分钟就够了
The Prospects of the Metaverse and the Four Tracks
Image information extraction DEM
【AGC】开放式测试示例
【HMS core】【FAQ】push kit、WisePlay DRM、Location Kit、Health Kit、3D Modeling Kit、SignPal Kit典型问题合集4
Golang分布式应用定时任务如何实现