javascript 两个不带进位的正整数

 

函数名称为: add_two_int_without_carrying(n1, n2),

其中(n1, n2)是函数的参数。就是个位与个位相加,

十位与十位相加  以此类推,

函数返回相加的结果,没相加则不返回。

 

例如100+22就等于22,因为百位没有进行相加,而且不能进位,例如22+19=31

 

function add_two_int_without_carrying(n1, n2) {
var result = 0,
x = 1;
while (n1 > 0 && n2 > 0) {
result += x * ((n1 + n2) % 10);
n1 = Math.floor(n1 / 10);
n2 = Math.floor(n2 / 10);
x*= 10;
}
return result;
}

posted @ 2021-05-04 08:33  咸瑜  阅读(53)  评论(0编辑  收藏  举报