Created
January 13, 2014 22:40
-
-
Save areagray/8409533 to your computer and use it in GitHub Desktop.
Coderbyte Challenge SecondGreatLow
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
/* Coderbyte Challenge | |
Using the JavaScript language, have the function SecondGreatLow(arr) take the array of numbers stored in arr | |
and return the second lowest and second greatest numbers, respectively, separated by a space. | |
For example: if arr contains [7, 7, 12, 98, 106] the output should be 12 98. | |
The array will not be empty and will contain at least 2 numbers. | |
*/ | |
function SecondGreatLow(arr) { | |
var secLowest, | |
secHighest; | |
//sort it up | |
arr.sort(function(a, b) { | |
return a - b ; | |
}); | |
//grab the vals | |
secLowest = arr[1]; | |
secHighest = arr[arr.length-2]; | |
//send em back | |
return seclowest + ' ' +secHighest; | |
} | |
// keep this function call here | |
// to see how to enter arguments in JavaScript scroll down | |
SecondGreatLow([4, 90]); |
@Shahab0622 his code did not generate unique values. You can use a for loop to find unique values or define a function (see discussion on Stack Overflow http://stackoverflow.com/questions/1960473/unique-values-in-an-array/14438954#14438954).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how do you resolve for situations as in the example, where the 7 is repeated twice, and you have to capture 12 in your answer?