当前位置:网站首页>reduce错误示范
reduce错误示范
2022-07-27 20:39:00 【yibucuo】
求十进制数,转2进制后,含有1个个数
错误示范:
var one = 5;
var str = one.toString(2).split("");
var num = str.reduce(function (res, item) {
if (item == 1) {
console.log(res) // 0 undefined
return res + 1;
}
}, 0);
正确示范:
var one = 5;
var str = one.toString(2).split("");
var num = str.reduce(function (res, item) {
if (item == 1) {
return res + 1;
} else {
return res
}
}, 0);
console.log(num) // 2
边栏推荐
- containerd ctr运行ansible容器执行ansible-playbook任务完整命令
- 我年薪100万,全身上下没有超过100块的衣服:存钱,是最顶级的自律
- Containerd CTR run the ansible container and execute the complete command of ansible playbook task
- Cloud native enthusiast weekly: a complete collection of client go examples
- Excel VBA finds out the maximum and minimum values of a column of time, and repeatedly pastes multiple values according to the actual situation
- Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
- 51 MCU internal peripherals: real time clock (SPI)
- Helm chart explanation and common commands: helm template / package / plugin
- Complete Guide to IOT architecture
- 毕设-基于SSM高校后勤管理系统
猜你喜欢

How to narrow the gap between project planning and implementation?

强化学习——PyTorch 实现 Advantage Actor-Critic (A2C)

Excel VBA finds out the maximum and minimum values of a column of time, and repeatedly pastes multiple values according to the actual situation

On data management of data warehouse

Deploy dolphin scheduler high availability cluster based on rainbow

Flink怎么使用Savepoint

Analysis of cloud native application security organization structure

Main security risks and Countermeasures of cloud computing services

Do you want to be dismissed? Let's take a look at the "exit tips" of programmers
软件测试功能测试全套常见面试题【功能测试】面试总结4-2
随机推荐
毕设-基于SSM高校后勤管理系统
置信区间之正态
Vulnhub range double trouble
Lanproxy映射本地开发环境
在所有浏览器中禁用带有元 HTML 标记的缓存
【 图像去雾】基于暗通道和非均值滤波实现图像去雾附matlab代码
Xu Jinbo: AI protein prediction and design
Main security risks and Countermeasures of cloud computing services
Node-RED系列(三十):使用持久化ui-table 刷新页面不清空上一次的table数据
51 MCU internal peripherals: real time clock (SPI)
Take byte offer in four rounds and answer the interview questions
kubevela通过CLI部署应用
测试文章
XML 外部实体 (XXE) 漏洞及其修复方法
Harmonyos third operation
How do I shut down oracle?
许锦波:AI蛋白质预测与设计
On data management of data warehouse
After returning to mixlab for three days, "creative team" cured my spiritual internal friction
Node red series (30): use persistent UI table to refresh the page without emptying the last table data