Comments (4)
这个是最简洁的
from exercise2.
添加注释,方便理解:
function sumStrings(a,b){
var res = '', // 存储最终结果
c = 0; // 假设想加结果>=10,那么把10位缓存起来
// 将字符串切割成数组,每一位单独相加
a = a.split('')
b = b.split('')
// 不断循环相加,直到所有数字都加完为止
while (a.length || b.length || c) {
/*
一、c为Boolean:
1. true与数字n相加,相当于1+n
2. false与数字n相加,相当于0+n
二、[Bitwise NOT (~)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT)
1. 数字进行~~运算,例如~~3的结果为3
2. a和b两个数组长度可能不一致,其中一个会先清空
3. 假设a为[],a.pop()为undefined
4. ~~undefined的结果为0
三、c += ~~a.pop() + ~~b.pop() 可以计算出每一位相加的结果
*/
c += ~~a.pop() + ~~b.pop()
/*
1. c的结果可能>=10,但每一位只能存储0-9的数字,因此要用c % 10取个位数
2. 每位数要存储在结果的首尾
*/
res = (c % 10) + res
/*
判断c是否>=10,并进行下一次相加
*/
c = c > 9
}
return res.replace(/^0+/, '')
}
from exercise2.
添加注释,方便理解:
function sumStrings(a,b){ var res = '', // 存储最终结果 c = 0; // 假设想加结果>=10,那么把10位缓存起来 // 将字符串切割成数组,每一位单独相加 a = a.split('') b = b.split('') // 不断循环相加,直到所有数字都加完为止 while (a.length || b.length || c) { /* 一、c为Boolean: 1. true与数字n相加,相当于1+n 2. false与数字n相加,相当于0+n 二、[Bitwise NOT (~)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT) 1. 数字进行~~运算,例如~~3的结果为3 2. a和b两个数组长度可能不一致,其中一个会先清空 3. 假设a为[],a.pop()为undefined 4. ~~undefined的结果为0 三、c += ~~a.pop() + ~~b.pop() 可以计算出每一位相加的结果 */ c += ~~a.pop() + ~~b.pop() /* 1. c的结果可能>=10,但每一位只能存储0-9的数字,因此要用c % 10取个位数 2. 每位数要存储在结果的首尾 */ res = (c % 10) + res /* 判断c是否>=10,并进行下一次相加 */ c = c > 9 } return res.replace(/^0+/, '') }
点赞👍
from exercise2.
膜拜
from exercise2.
Related Issues (4)
- jspref 比较 HOT 13
- 大数相加参考资料 HOT 1
- 大数相加问题以及jsPerf性能测试报告 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from exercise2.