-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsum-of-two-lowest-positive-integers.js
62 lines (54 loc) · 2.23 KB
/
sum-of-two-lowest-positive-integers.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
function sumTwoSmallestNumbers(numbers) {
// find the two smallest numbers
// a place to store the smallest number
let smallest;
// a place to store the 2nd smallest number
let secondSmallest;
// compare the first two numbers in the array
if (numbers[0] < numbers[1]) {
smallest = numbers[0]
secondSmallest = numbers[1]
} else {
smallest = numbers[1]
secondSmallest = numbers[0]
}
// iterate over numbers starting with the 3rd number
for (let i = 2; i < numbers.length; i++) {
const currentNumber = numbers[i];
// if the current number is smaller than the smallest number
if (currentNumber < smallest) {
// set smallest number to be the 2nd smallest number
secondSmallest = smallest;
// set the current number to be the smallest number
smallest = currentNumber;
} else if (currentNumber < secondSmallest) {
// else if the current number is smaller than the 2nd smallest number
// set the 2nd smallest number to the current number
secondSmallest = currentNumber;
}
}
console.log(smallest, secondSmallest);
// return the sum of the 2 smallest numbers
return smallest + secondSmallest;
}
function sumTwoSmallestNumbers(numbers) {
const smallest = Math.min.apply(null, numbers);
const smallestIndex = numbers.indexOf(smallest);
const numbersWithoutSmallest = numbers.slice();
numbersWithoutSmallest.splice(smallestIndex, 1);
const secondSmallest = Math.min.apply(null, numbersWithoutSmallest);
return smallest + secondSmallest;
}
function sumTwoSmallestNumbers(numbers) {
const smallest = Math.min(...numbers);
const smallestIndex = numbers.indexOf(smallest);
const numbersWithoutSmallest = numbers.slice();
numbersWithoutSmallest.splice(smallestIndex, 1);
const secondSmallest = Math.min(...numbersWithoutSmallest);
return smallest + secondSmallest;
}
console.log(sumTwoSmallestNumbers([5, 8, 12, 19, 22]), 13, "Sum should be 13");
console.log(sumTwoSmallestNumbers([15, 28, 4, 2, 43]), 6, "Sum should be 6");
console.log(sumTwoSmallestNumbers([3, 87, 45, 12, 7]), 10, "Sum should be 10");
console.log(sumTwoSmallestNumbers([23, 71, 33, 82, 1]), 24, "Sum should be 24");
console.log(sumTwoSmallestNumbers([52, 76, 14, 12, 4]), 16, "Sum should be 16");