when you pass an argument full of numbers into a function, how do you make the function return the largest number in the arguments? please i need this explained without the function actually defining/declaring any of the arguments/parameters in its parenthesis
You use the arguments property, which you can research.
i have but i cant get it working. i can only get it working when i reference the arguments in my function call. but i want a way to do this without defining parameters, incase the arguments passed into the function exceeds the function parameters. heres what i mean
onclick="values(3,6,8);" will be passed to my function which is ready for them: function values(a,b,c).
but what if my function wasnt ready, i.e more arguments where passed, like values(3,6,8,24,4) and their length exceeded my function parameters? i want a way to make the function be able to handle this and still work out the max number.
im happy with the Math.max method but i would just like to see how this is done
The arguments object in a function is an Array-like object. It has a length property telling you how many arguments the function currently has passed in. You can also access each argument in array syntax:
<script type="text/javascript">
function maxNumber()
{
var max = '';
for(var i in arguments)
{
if(arguments[i] > max)
max = arguments[i];
}
alert(max);
}
maxNumber(1, 8, 5, 9, 3, 2);
</script>
Bookmarks