PDA

View Full Version : I need a function to getMaximum number



liffland
04-20-2005, 02:57 PM
function getMax()

ddadmin
04-20-2005, 06:15 PM
Get Maximum number in relation to what? A set of numbers fed in as parameters? You need to be more specific. :)

liffland
04-21-2005, 01:16 PM
Sorry, yes, lets say we have tree numbers.

ddadmin
04-21-2005, 06:15 PM
Well, there are many ways to do this. Here's a generic function that finds the largest number passed into its parameters, and returns it:


<script type="text/javascript">

function largestnumber(){
var largest=arguments[0]
for (i=0; i<arguments.length; i++){
if (arguments[i]>largest)
largest=arguments[i]
}
return(largest)
}

</script>

Usage example:


alert(largestnumber(3, 5, 9, 1, 3, 2)) alerts 9
alert(largestnumber(23, 42, 12)) alerts 42

mwinter
04-21-2005, 06:45 PM
Well, there are many ways to do this.But the most sensible is to use the Math.max method, which can take any number of arguments, and is native (that is, quicker).


var largest = Math.max(a, b, c, ...);Mike

ddadmin
04-21-2005, 07:18 PM
But the most sensible is to use the Math.max method, which can take any number of arguments, and is native (that is, quicker).


var largest = Math.max(a, b, c, ...);Mike

Hehe you're right, completely forgot about Math.max(). Actually, I did think of it, but for some reason remember it as only accepting two parameters.

liffland
04-24-2005, 07:24 PM
What if I prompt the user to insert 3 numbers and then display the maximum number
I tried to do it but it's not working

mwinter
04-24-2005, 09:13 PM
What if I prompt the user to insert 3 numbers and then display the maximum number
I tried to do it but it's not workingCould you please link to, or post, what you've tried so far.

Mike

liffland
04-25-2005, 01:54 PM
<html>
<head>
<title>Get Maximum Number</title>


<script type="text/javascript">

function getMax(num1, num2, num3){
var largest=arguments[num1, num2, num3]
for (i=0; i<arguments.length; i++){
if (arguments[i]>largest)
largest=arguments[i]
}
return(largest)
}

</script>


</head>
<body>
<h2>Get Maximum Number</h2>


<script language="JavaScript" type="text/javascript"><!--
var num1=prompt("Enter a number: ", "")
var num2=prompt("Enter another number: ", "")
var num3=prompt("Enter the third number: ", "")
var max= getMax(num1, num2, num3)
document.write("You entered: ", num1, ", ", num2, ", ", num3, ", and the Maximum number is: ", max)

// -->
</script>

</body>
</html>

liffland
04-25-2005, 02:17 PM
sorry I give the wrong script
here is the one I want you to see
<html>
<head>
<title>Get Maximum Number</title>


<script type="text/javascript">

function getMax(num1, num2,num3){
var largest=arguments[num1, num2, num3]
for (i=0; i<arguments.length; i++){
if ((num1>num2)&&(num1>num3))
largest=num1
else if((num2>num1)&&(num2>num3))
largest=num2
else
largest=num3
}
return(largest)
}

</script>


</head>
<body>
<h2>Get Maximum Number</h2>


<script language="JavaScript" type="text/javascript"><!--
var num1=prompt("Enter a number: ", "")
var num2=prompt("Enter another number: ", "")
var num3=prompt("Enter the third number: ", "")
var max= getMax(num1, num2, num3)
document.write("You entered: ", num1, ", ", num2, ", ", num3, ", and the Maximum number is: ", max)

// -->
</script>

</body>
</html>

mwinter
04-25-2005, 07:38 PM
function getMax(num1, num2,num3){
var largest=arguments[num1, num2, num3]I think you misunderstand how the arguments object works.

As you no doubt know, you can specify formal, named arguments to a function and retrieve the values of those arguments using the given names. However, a function can be called with any number of arguments, even if that means there are more, or less, than the number of names. So, how would you access the extra, unnamed values? Using the arguments object.

The object has a few named properties: length, callee, plus the properties that are present on all built-in objects. The arguments to the function are available in numbered properties: 0 to length-1.

The function that ddadmin posted would take any number of arguments without modification. However, so will the built-in Math.max method, and should do so quicker.


<script language="JavaScript" type="text/javascript"><!--The language attribute is deprecated and redundant. Don't use it. Similarly, SGML comments (<!-- -->) in this context are outdated and should not be included.


var num1=prompt("Enter a number: ", "")
var num2=prompt("Enter another number: ", "")
var num3=prompt("Enter the third number: ", "")
var max= getMax(num1, num2, num3)The prompt function returns a string. When you compare strings, the operation is lexical: characters are compared based on their code point values. They will not be compared as the number values they might represent. The built-in max method will automatically convert its arguments to numbers (if possible). However, you should probably check that the string is a number before using it.


function getNumber(text) {var n;
do {
n = prompt(text);
} while(n && !/^\d+$/.test(n));

return n ? +n : NaN;
}

var num1 = getNumber('Enter a number:'),
num2 = getNumber('Enter another number:'),
num3 = getNumber('Enter a third number:');

document.write([
'You entered: ',
num1, ', ',
num2, ', ',
num3, '. ',
'The largest is ',
Math.max(num1, num2, num3),
'.'
].join(''));The function getNumber returns a number or NaN (not-a-number). If the user enters a string that is not numeric, they are prompted again. The user can select cancel (or enter an empty string), which returns NaN. The code above doesn't check for these "cancelled" conditions, but you could if you want to using the isNaN built-in function.

Your other question regarding average values will obviously be similar, just replacing 'largest' with 'average', and

Math.max(num1, num2, num3),

with

(num1 + num2 + num3) / 3,

Notice the comma (,).

Hope that helps,
Mike