-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBeginner_Series3.js
38 lines (29 loc) · 938 Bytes
/
Beginner_Series3.js
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
36
37
// Kata link:
// https://www.codewars.com/kata/55f2b110f61eb01779000053/
// -------------------------------------
// Instructions:
/*
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
*/
// -------------------------------------
// Solution 1:
function getSum(a, b) {
if (a == b) {
return a;
}
let min = Math.min(a, b);
let max = Math.max(a, b);
return (max - min + 1) * (min + max) / 2;
}
// -------------------------------------
// Basic Tests
console.log(getSum(0, -1) == -1);
console.log(getSum(0, 1) == 1);