V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  jiaqiu2003  ›  全部回复第 1 页 / 共 1 页
回复总数  6
@phpfpm 无限进位。。。又 miss 掉一个点。放弃
@phpfpm 麻烦看下这个。进位的那个算是抄袭吧。。。实在没想到好的
function add(a, b) {
if (a.length > b.length) {
return add(b, a);
}
const prefix = b.substring(0, b.length - a.length - 1);
return prefix + `0${a}`.split('').reverse().reduce((acc, s, idx) => {
const carray = acc.length - idx;
return s * 1 + b[b.length - idx - 1] * 1 + carray + acc.substring(carray);
}, '');
}
@phpfpm 感谢~
我再想一个
function add(str1, str2) {
const getNum = str => str.split('').reverse().reduce((acc, s, idx) => acc + (s * 10 ** idx), 0);
return getNum(str1) + getNum(str2) + '';
}
add('123', '21'); // Print '144'
2020-05-27 11:05:19 +08:00
回复了 fensou 创建的主题 生活 你有用钱解决不了的烦心事吗?
有,爱上一个不爱我的人。
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   906 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 14ms · UTC 22:55 · PVG 06:55 · LAX 15:55 · JFK 18:55
Developed with CodeLauncher
♥ Do have faith in what you're doing.