Write a function called twoHighest that takes an array of numbers as its argument and returns the two highest numbers within the array.
The returned value should be an array in the following format: [secondHighest, highest]
The order of the numbers passed in could be any order.
Do not use the build in sort() method - the tests will fail!
function twoHighest(arr) {
var highest = 0;
var secondHighest = 0;
for (i = 0; i < arr.length; i++) {
if (arr[i] > highest) {
highest = arr[i];
}
}
for (i = 0; i < arr.length; i++) {
if (arr[i] > secondHighest && arr[i] < highest) {
secondHighest = arr[i];
}
}
return [secondHighest, highest];
}
console.log(twoHighest([1, 2, 10, 8])); // [8, 10]
console.log(twoHighest([6, 1, 9, 10, 4])); // [9,10]
console.log(twoHighest([4, 25, 3, 20, 19, 5])); // [20,25]
console.log(twoHighest([1, 2, 2])); // [2, 2];
This works until the last array [1, 2, 2,]. It is returning [1, 2] rather than [2, 2].