-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraySolution.js
More file actions
32 lines (27 loc) · 778 Bytes
/
arraySolution.js
File metadata and controls
32 lines (27 loc) · 778 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
/*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)
(1, 0) --> 1 (1 + 0 = 1)
(1, 2) --> 3 (1 + 2 = 3)
(0, 1) --> 1 (0 + 1 = 1)
(1, 1) --> 1 (1 since both are same)
(-1, 0) --> -1 (-1 + 0 = -1)
(-1, 2) --> 2 (-1 + 0 + 1 + 2 = 2) */
function getSum( a,b ){
if(a == b){
return a
}
if(b>a){
let array =[]
for(let i = a ; i <=b ; i++){
array.push(i)
}
return array.reduce((acc,c)=>acc+c,0)
}else{
let array =[]
for(let i = b ; i <=a ; i++){
array.push(i)
}
return array.reduce((acc,c)=>acc+c,0)
}
}