-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum-of-numbers.js
More file actions
35 lines (30 loc) · 812 Bytes
/
sum-of-numbers.js
File metadata and controls
35 lines (30 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Given two integers a and b, which can be positive or negative, find the sum of all the integers between and including them and return it. If the two numbers are equal return a or b.
// Note: a and b are not ordered!
// Examples (a, b) --> output (explanation)
function getSum( a,b )
{
if (a === b) {
return 0;
}
else if(a > b) {
let total = 0;
let totalArray = [];
for(b; b <= a; b++) {
totalArray.push(b)
total += b;
}
return total
}
else if(a <= b) {
let total = 0;
let totalArray = [];
for(a; a <= b; a++) {
totalArray.push(a)
total += a;
}
return total
}
}
console.log(getSum(-5,1))
console.log(getSum(15,15))
console.log(getSum(1,3))