Programming Questions

SecondGreatLow Instructions?

function SecondGreatLow(arr) { arr.sort(function(a, b){return a - b}); var str = "" if(arr.length == 2){ str = arr[0] + " " + arr[1]; } else if(arr.length > 2){ var lowest = arr.shift(); var highest = arr.pop(); for(var i = 0; i < arr.length; i++){ if(arr[i] == lowest){ var lowest2 = arr.shift(); } if(arr[arr.length - 1] == highest){ var highest2 = arr.pop(); } } } return arr[0] + " " + arr[arr.length - 1];It says my test case was wrong in [2,2,2,5,5,5,6] - but, since there are THREE numbers in this array, there IS NOT a "second greatest" or "second lowest." What are we supposed to do?

cscholberg
posted this question on 9/25/14 **|**

3

You have three numbers of different values. The greatest is 6, the second greatest is 5. The lowest is 2, the second lowest is 5.

mattlarsh

answered on 09/25/14

0

Your using javascript so here are the steps your code should follow (I think from memory)
1. Loop through the array removing double ups (Make a new array of unique numbers)
2. Sort that array largest to smallest
3. Then find the second from each end

MarshallOfSound

answered on 09/26/14

Log in to write an answer.