Skip to content

Commit

Permalink
✨ Implement three largest numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
matssom committed Jun 17, 2021
1 parent 0d0b544 commit dede27c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions js/easy/three_largest_numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Return three largest numbers of an array with
// at least three numbers

function findThreeLargestNumbers(array) {
let smallest, medium, largest
for (const e of array) {
if (e > largest || !largest) {
smallest = medium
medium = largest
largest = e
}
else if (e > medium || !medium) {
smallest = medium
medium = e
}
else if (e > smallest || !smallest) {
smallest = e
}
}
return [smallest, medium, largest]
}

0 comments on commit dede27c

Please sign in to comment.