Created
June 30, 2020 19:15
-
-
Save andreiskandar/86e9974203f76930173b044a6133c730 to your computer and use it in GitHub Desktop.
Sum two largest numbers in the array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
In this exercise, we will be given an array of 2 or more numbers. | |
We will then have to find the two largest numbers in that array, and sum them together. | |
Input | |
const sumLargestNumbers = function(data) { | |
// Put your solution here | |
}; | |
console.log(sumLargestNumbers([1, 10])); | |
console.log(sumLargestNumbers([1, 2, 3])); | |
console.log(sumLargestNumbers([10, 4, 34, 6, 92, 2])); | |
Expected Output | |
11 | |
5 | |
126 | |
*/ | |
const sumLargestNumbers = function(data) { | |
let largest = data[0]; | |
let secondLargest = data[1]; | |
if(largest > secondLargest) { | |
largest = data[0]; | |
secondLargest = data[1]; | |
} else { | |
secondLargest = data[0]; | |
largest = data[1]; | |
} | |
for(let i = 2; i < data.length; i++){ | |
if(data[i] > largest) { | |
secondLargest = largest; | |
largest = data[i]; | |
} | |
} | |
return largest + secondLargest; | |
} | |
console.log(sumLargestNumbers([1, 2, 3])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
assignment