Click to See Complete Forum and Search --> : function


per
12-14-2003, 04:17 AM
Hi there,

i have got this code:

<HTML>
<HEAD>
<TITLE>Web Page containing JavaScript</TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
function largest(no1, no2, no3){
var biggest
no1=parseFloat(no1)
no2=parseFloat(no2)
no3=parseFloat(no3)
biggest=no1
if (no2>biggest) {
biggest=no2
}
if (no3>biggest) {
biggest=no3
}
return biggest
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<H1><FONT COLOR="BLUE">A Function To Calculate The Largest Value</FONT></H1>
<SCRIPT>
<!--
var number1, number2, number3, max
number1=window.prompt("What is your first number","?")
number2=window.prompt("What is your second number","?")
number3=window.prompt("What is your third number","?")
max=largest(number1, number2, number3)
window.alert("The largest number entered was " + max)
//-->
</SCRIPT>
</BODY>
</HTML>

and now i was wanting to configure it a bit with these details:

Add a second function to calculate the average of the numbers entered, and a third to calculate the minimum value.

Use these functions, so that after alerting the user of the largest number entered, it also alerts him/ her of the average and smallest values as well.


thanks for any help

Pittimann
12-14-2003, 04:33 AM
Hi!

Being prompted three times - ok - if you chose this way, to get the numbers. Being alerted another three times is too much (I personally hate that)!

Sorry - I put your results in a single alert:

<HTML>
<HEAD>
<TITLE>Web Page containing JavaScript</TITLE>
<script language="JavaScript" type="text/javascript">
<!--
var number1, number2, number3;
number1=window.prompt("What is your first number","?");
number2=window.prompt("What is your second number","?");
number3=window.prompt("What is your third number","?");
result=calc(number1, number2, number3);
window.alert(result);
function calc(no1, no2, no3){
var biggest;
var smallest;
var average;
no1=parseFloat(no1);
no2=parseFloat(no2);
no3=parseFloat(no3);
biggest=no1;
smallest=no1;
if (no2>biggest) {
biggest=no2;
}
if (no3>biggest) {
biggest=no3;
}
if (no2<smallest) {
smallest=no2;
}
if (no3<smallest) {
smallest=no3;
}
average=(no1+no2+no3)/3;
result="The largest number entered was "+biggest+";\nthe smallest number entered was "+smallest+";\nthe average of the three numbers is "+average+".";
return result;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<H1><FONT COLOR="BLUE">A Function To Calculate The Largest Value</FONT></H1>
</BODY>
</HTML>

Cheers - Pit