-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumAll.js
68 lines (44 loc) · 1.4 KB
/
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
68
/*** 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.
/* Third Solution */
function sumAll(arr) {
const min = Math.min(arr[0], arr[1]);
const max = Math.max(arr[0], arr[1]);
let total = 0;
for (let i = min; i <= max; i++) {
total += i;
}
return total;
}
/********************************************************************************/
/* Second Solution */
function sumAll(arr) {
const sortedArray = arr.sort((a,b) => a - b);
const lastNum = sortedArray[sortedArray.length-1];
let total = 0;
for (let i = sortedArray[0]; i <= lastNum; i++) {
total += i;
}
return total;
}
/********************************************************************************/
/* First Solution */
function sumAll(arr) {
// create an empty array
var numArr = [];
// get the smallest number from the array
var min = Math.min(arr[0], arr[1]);
// get the largest number from the array
var max = Math.max(arr[0], arr[1]);
// loop through the array and add the range of numbers between min
// ..and max to the numArr array
for (var i = min; i <= max; i++) {
numArr.push(i);
}
// return the sum of all numbers from the numArr array
return numArr.reduce(function(a,b) {
return a + b;
});
}
sumAll([1, 4]);