0
点赞
收藏
分享

微信扫一扫

codewar ----- 累加

爱动漫建模 2022-09-13 阅读 80


Given two integers ​​a​​​ and ​​b​​​, which can be positive or negative, find the sum of all the numbers between including them too and return it. If the two numbers are equal return ​​a​​​ or ​​b​​.

Note: ​​a​​​ and ​​b​​ are not ordered!

Examples

GetSum(1, 0) == 1   // 1 + 0 = 1
GetSum(1, 2) == 3 // 1 + 2 = 3
GetSum(0, 1) == 1 // 0 + 1 = 1
GetSum(1, 1) == 1 // 1 Since both are same
GetSum(-1, 0) == -1 // -1 + 0 = -1
GetSum(-1, 2) == 2 // -1 + 0 + 1 + 2 = 2

我的

function GetSum( a,b )
{
var sum=0;
if(a==b)
return a;
if(a<b)
{
for(var i=a;i<=b;i++)
sum += i;
}
if(a>b)
{
for(var i=b;i<=a;i++)
sum += i;
}
return sum;
}

 

others:

 

 

 

 

 

 

 

举报

相关推荐

0 条评论