/    Sign up×
Community /Pin to ProfileBookmark

using min max functions

I have a small function that I am not sure how to proceed with..

I have four values of which I want the average of the middle two values

Something like this;

[code]
document.menter.Final.value = ( j1 + j2 + j3 + j4 – max (j1,j2,j3,j4) -min (j1,j2,j3,j4) ) /2 ;
[/code]

But as I understand it the min max functions only work with two values. So I am not sure how to do this. Thanks for your assistance.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Orc_ScorcherJan 26.2006 — In recent JavaScript versions, Math.min and Math.max can take any number of arguments. Just prefix max and min in your code with 'Math.'.
Copy linkTweet thisAlerts:
@DasherauthorJan 26.2006 — I can't find a reference that allows more than 2 values in the min max compare. So I tried this...
<i>
</i>Cmax1 = Math.max(jone,jtwo);
Cmax2 = Math.max(jthree,jfour);
CmaxF = Math.max(Cmax1,Cmax2);
Cmin1 = Math.min(jone,jtwo);
Cmin2 = Math.min(jthree,jfour);
CminF = Math.min(Cmin1,Cmin2);
Cmedian = (jone + jtwo + jthree + jfour - CmaxF - CminF);
document.menter.Final.value = Cmedian / 2 ;

But I get nonsense results like 5050495 for 10, 10, 10, 10.

[URL=http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Math:max]On this page[/URL]

I found the comment "Because max is a static method of Math, you always use it as Math.max(), rather than as a method of a Math object you created."

I have no idea what that really statement means, but evidently I am doing it wrong. :-)
Copy linkTweet thisAlerts:
@SelrachJan 26.2006 — Can we see the code for the page where you are calling this function? It looks like the code is right, but how are you setting jone, jtwo, jthree, and jfour?
Copy linkTweet thisAlerts:
@DasherauthorJan 26.2006 — The values are inputted thusly;

in a form
<i>
</i>&lt;form name="menter" action="&lt;?php echo $PHP_SELF ?&gt;" method="post"&gt;
&lt;input value="0" name="J1" type="text" size='2' onFocus="startCalc();" onBlur="stopCalc();" /&gt;
// etc for each Jx
&lt;/form&gt;
// ******************
// in the javascript section
function calc(){
jone = document.menter.J1.value;
//etc for each jxxx.
}


FYI: The actual function is a bit more complicated. There is a switch statement that selects different math operations such as jone+jtwo+jthree or (jone +jtwo) /2 All of the switch statements work correctly except this one with the Math.max and Math.min .

I could post the enter javascript if it would help. I am thinking somehow I am not using the Math.max or Math.min methods correctly. I am not a frequent user of javascript. Most of my programming is done in PHP.

There are two other functions not shown: startCalc and stopCalc they work fine.
Copy linkTweet thisAlerts:
@Orc_ScorcherJan 26.2006 — The values of input elements are strings, you need to convert them to numbers first. And trust me, max and min can handle more than 2 arguments. If you need a reference, http://windowssdk.msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/f3ea1b8a-5fd0-482a-971b-b7f8e2b9b7eb.asp
Copy linkTweet thisAlerts:
@DasherauthorJan 26.2006 — [B]Kaa_Ching [/B]

That was it! Works great now.

Thanks so much.

That would explain why I had to add quotes around my case 'x': statements too.
×

Success!

Help @Dasher 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...