/    Sign up×
Community /Pin to ProfileBookmark

How to Return "n"th highest/lowest number

Daê Galera!!! (<-local language)
Here is the noob again.
Is there ne simple code that returns the 2nd Highest/lowest number or the 3rd… As Math.max(…) returns the 1st?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JonaApr 22.2004 — [font=monospace]

<script type="text/javascript">

var nums = [21, 32, 45];

maxNums = Math.max(nums[0], nums[1]);

maxNums2 = Math.ceil(maxNums, nums[2]);

document.write(maxNums2);

</script>

[/font]
Copy linkTweet thisAlerts:
@PittimannApr 22.2004 — Hi!

Something to play with:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
var nums0 = [21, -3.1, 111, 45.3, 11, 32.99, 45];
function compareNumbers(a, b) {
return a - b;
}
function showMinMax(what, val){
var nums = nums0;
nums=nums.sort(compareNumbers);
if(what==0){
alert(nums[nums.length-val]);
}
if(what==1){
nums=nums.reverse();
alert(nums[nums.length-val]);
}
}
</script>
</head>
<body>
<form>
<script language="JavaScript" type="text/javascript">
<!--
document.write('Numbers: '+nums0+' - please select your "n" in the dropdown:<br>');
document.write('<select name="which">');
for (var i=1;i<=nums0.length;i++){
document.write('<option value='+i+'>'+i+'</option>');
}
//-->
</script>
</select>
<input type=button onclick="showMinMax(0,this.form.which.value)" value="nth max">
<input type=button onclick="showMinMax(1,this.form.which.value)" value="nth min">
</form>
</body>
</html>
[/code]

Cheers - Pit
Copy linkTweet thisAlerts:
@BrasucaauthorApr 22.2004 — Pittiman, Are u really ignorant? I wish to be as ignorant as u in javascript! ? u cast a no cost code, caring the count of combinations. (eeek. bad english.. better do those in PT-?

Thks also, Jona.. but the way ya did i believe i it would cost 1 more line for each number... and i need like 25 numbers!
Copy linkTweet thisAlerts:
@PittimannApr 22.2004 — Hi!

You're welcome! Do you need a hand to adjust things for your needs or is it ok like that?

Cheers - Pit
Copy linkTweet thisAlerts:
@JonaApr 22.2004 — [i]Originally posted by Brasuca [/i]

[B]Thks also, Jona.. but the way ya did i believe i it would cost 1 more line for each number... and i need like 25 numbers! [/B][/QUOTE]


[font=arial]I didn't know what you were looking for, so I just put out a suggestion type of thing.[/font]
Copy linkTweet thisAlerts:
@BrasucaauthorApr 22.2004 — Np jona, the fault was mine. I wasn't enough clear ?.

Pittiman, no need... from now and on i think i can go on alone. If i need ne help i mail ya or post another msg.

I need to learn how to fish... hehehehehe

Says my signature: Nothing more than a noob brasilian
×

Success!

Help @Brasuca spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...