1) Why does the function smallest
fail? I think it conforms to the example in the Mozilla documentation , which says
function getMin(x,y) { //from Mozilla documentation
return Math.min(x,y)
}
function smallest(array){ //my own experimentation with Resig`s example
return Math.min(array);
}
function largest(array){ //from John Resig`s learning advanced JavaScript #41
return Math.max.apply( Math, array );
}
assert(smallest([0, 1, 2, 3]) == 0, "Locate the smallest value.");
assert(largest([0, 1, 2, 3]) == 3, "Locate the largest value.");
1) Why does the function smallest
fail? I think it conforms to the example in the Mozilla documentation https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Math/min, which says
function getMin(x,y) { //from Mozilla documentation
return Math.min(x,y)
}
function smallest(array){ //my own experimentation with Resig`s example
return Math.min(array);
}
function largest(array){ //from John Resig`s learning advanced JavaScript #41
return Math.max.apply( Math, array );
}
assert(smallest([0, 1, 2, 3]) == 0, "Locate the smallest value.");
assert(largest([0, 1, 2, 3]) == 3, "Locate the largest value.");
Share
Improve this question
edited Mar 25, 2011 at 0:39
Pointy
414k62 gold badges594 silver badges627 bronze badges
asked Mar 25, 2011 at 0:29
mjmitchemjmitche
2,0676 gold badges25 silver badges31 bronze badges
6 Answers
Reset to default 5The "Math.min()" function can take an arbitrary number of arguments. However, passing a single array to the function is just passing in one argument, not several. That single argument is the array itself. Arrays, you will recall, are first-rate values.
That's the whole point of the "apply" function that's available on every function. It is used to invoke the function and pass it an argument list posed of entries in an array instance. When you do this:
var array = [ 1, 2, 3, 4, 5 ];
someFunction.apply(irrelevant, array);
that's like calling:
someFunction(1, 2, 3, 4, 5);
(The "irrelevant" argument is what's to be used as the this
value in the function call.)
Math.min
and Math.max
takes any number of numbers, as arguments. Your smallest
is trying to pass an array, not numbers.
The use of apply
(as in largest
in your example) pastes the elements of the array as arguments.
It doesn't conform to the example. The []'s in the documentation are optional indicators, not array delimiters.
Try
function smallest(array) { return Math.min.apply(Math, array); }
It won't work when passed as an array because min
converts its arguments to numbers before paring. Trying to convert an array to a number will, unless the array contains a single element that looks like a number, produce NaN
.
So
Math.min([3]) === 3
Math.min('3') === 3
but
isNaN(Math.min([3, 4])) // because,
isNaN(Math.min('3,4')) // which is the same as
isNaN(Math.min(+'3,4')) // which is equivalent to
isNaN(Math.min(NaN))
.apply( object, array )
is the correct syntax to unpack an array into an argument list. Just passing an array to a function that converts all arguments to a number isn't useful, since it will only see NaN.
apply passes the array to Math.min as an arguments object- Math.min(a,b,c,d,e)
smallest is passing the whole array as the first agument-
Math.min(x) and it really has nothing to pare it to.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741494633a4349978.html
评论列表(0条)