-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalg01-sumall.js
67 lines (58 loc) · 1.88 KB
/
alg01-sumall.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Source: <https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-numbers-in-a-range>
Sum All Numbers in a Range
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.
*/
const mySumAll = arr => {
const max = Math.max(...arr);
const min = Math.min(...arr);
let sum = 0;
for (let index = min; index <= max; index += 1) {
sum += index;
}
return sum;
}
const sum1 = mySumAll([4, 1]); //10
const sum2 = mySumAll([5, 10]); //45
console.log(sum1, sum2);
/*
Get a help > Get a hint <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-numbers-in-a-range/16083>
*/
//Solution 1
function sumAll1(arr) {
let max = Math.max(arr[0], arr[1]);
let min = Math.min(arr[0], arr[1]);
let sumBetween = 0;
for (let i = min; i <= max; i++) {
sumBetween += i;
}
return sumBetween;
}
//Solution 2
const sumAll2 = arr => {
// Buckle up everything to one!
const startNum = arr[0];
const endNum = arr[1];
// Get the count of numbers between the two numbers by subtracting them and add 1 to the absolute value.
// ex. There are |1-4| + 1 = 4, (1, 2, 3, 4), 4 numbers between 1 and 4.
const numCount = Math.abs(startNum - endNum) + 1;
// Using Arithmetic Progression summing formula
const sum = ((startNum + endNum) * numCount) / 2;
return sum;
};
//Solution 3
function sumAll3(arr) {
let sumBetween = 0;
for (let i = Math.min(...arr); i <= Math.max(...arr); i++) {
sumBetween += i;
}
return sumBetween;
}
//Solution 4
function sumAll4(arr) {
const [first, last] = [...arr].sort((a, b) => a - b);
return first !== last
? first + sumAll([first + 1, last])
: first;
}