Skip to content

Commit 6f01b52

Browse files
authored
Merge pull request #1091 from MaxMaxner/1052-find-three-largest-Integer-js
resolves ticket #1052 find three largest Integer in Javascript
2 parents b360c0b + a3b4f1f commit 6f01b52

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Arrays/find_three_largest_integers.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Write a function that takes in an array of at least three integers and,
3+
without sorting the input array, returns a sorted array of the three largest integers in the input array.
4+
5+
6+
Approach:
7+
8+
Before implementing the task logic the array will be checked to see if it meets the criteria.
9+
Here the input array will be checked to see if it´s at least 3 elements big and if it
10+
only contains integers.
11+
12+
After that the logic is implemented.
13+
Because the given array should not be sorted for this task, the used logic works as following:
14+
1. Initialize a result array with 3 values, each of these 3 is initialized as -Infinity
15+
2. Check if first element of array is bigger than the third result value
16+
2.1 If value is bigger set new value (will be true for the first check as initialized with -Infinity)
17+
2.2 shift current numbers in result array
18+
2.3 checks if element is bigger than value 2, and also value 1 - following above logic
19+
3. Sorty the result array in descending order and console logs the result.
20+
*/
21+
22+
function findThreeLargestInteger(array) {
23+
24+
/************************************************************************
25+
* This block checks if given array matches the criteria *
26+
************************************************************************/
27+
let arrayLength = 0;
28+
let onlyIntegers = true;
29+
// iterate over given array and check each element
30+
array.forEach(element => {
31+
// is element an integer
32+
if (!Number.isInteger(element)) {
33+
onlyIntegers = false;
34+
}
35+
// count length of array
36+
arrayLength++;
37+
});
38+
39+
40+
/************************************************************************
41+
* Implementing the task logic *
42+
************************************************************************/
43+
if (onlyIntegers && arrayLength >= 3) {
44+
// initialize result array with -Infinite
45+
let result = [-Infinity, -Infinity, -Infinity];
46+
47+
// Loop through the input array
48+
for (const num of array) {
49+
// Check if the current number is larger than the third largest number
50+
if (num > result[2]) {
51+
// Shift the current numbers down the result array
52+
result[0] = result[1];
53+
result[1] = result[2];
54+
result[2] = num;
55+
} else if (num > result[1]) {
56+
// Shift the current numbers down the result array
57+
result[0] = result[1];
58+
result[1] = num;
59+
} else if (num > result[0]) {
60+
// Shift the current number down the result array
61+
result[0] = num;
62+
}
63+
}
64+
// sorts the result in descending order (big to small)
65+
result.sort(function (a, b) { return b - a });
66+
console.log(result);
67+
68+
} else {
69+
console.log("The passed array does not match the expected criteria.");
70+
}
71+
72+
}
73+
74+
const testArray = [5, -3, 5, 100, 3, 55, 999, -18];
75+
const functionCheck = findThreeLargestInteger(testArray); // prints [999, 100, 55] to console
76+
77+
const arrayToShort = [100, 999];
78+
const unctionCheck1 = findThreeLargestInteger(arrayToShort); // prints "The passed array does not match the expected criteria." to console
79+
80+
const arrayNoInt = [100, 999, 8.5];
81+
const unctionCheck2 = findThreeLargestInteger(arrayNoInt); // prints "The passed array does not match the expected criteria." to console

0 commit comments

Comments
 (0)