We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them. The lowest number will not always come first.
For example, sumAll([4,1]) should return 10 because sum of all the numbers between 1 and 4 (both inclusive) is 10.
解决的思路如下:
function sumAll(arr) {
var big;
var small;
var sum =0;
if(arr[0]>=arr[1])
{
big = arr[0];
small = arr[1];
}
else{
big = arr[1];
small = arr[0];
}
for (let i = small;i<=big;i++)
{
sum+=i;
}
return sum;
}
console.log(sumAll([1, 4]));
sumAll([1, 4]);
Comments
Post a Comment