Code to find the average
function Avgfn () {
First convert the arguments into arrays
var ary = [];
for (var i = 0; i < arguments.length; i++) {
Ary.push (Arguments[i]);
Ary[ary.length] = arguments[i];
}
Array sorting
Ary.sort (function (A, b) {
return a-B;
});
Qiatouquwei
Ary.unshift ();
Ary.pop ();
Sum averages, decimals retain two-bit values
Eval () converts a string into a JS expression for us to use
Return (eval (ary.join ("+"))/ary.length). toFixed (2);
}
var res = AVGFN (2, 9, 0, 4, 5);
Console.log (RES);
*********************************************
Arguments.sort an error when sorting: Arguments.sort is not a function?
Sort is a method on the built-in class prototype of array, only the instance (array) of the array can use these methods, and although arguments looks like an array but not an array (class array), arguments is not an instance of the array class. So you can't use the Sort method directly ...
Console.log (arguments instanceof Array);//->false
How to convert an array of classes into a group
The slice method of the emulated array implements the simplest operation: Cloning an array
Array.prototype.slice = function () {
var ary = [];
The slice of the array is the implementation of the cloned
for (var i = 0; i < this.length; i++) {
Ary[ary.length] = This[i];//->ar Y.push (This[i]);
}
Converts the arguments to an array of
for (var i = 0; i < arguments.length; i++) {
Ary[ary . length] = Arguments[i];
}
return ary;
};
[34].slice];
rule: Although arguments is not an array, but it is an array of classes, that is, related operations are actually very similar to the group, such as loops, and so on are similar; in contrast to the above code we found that the built-in slice implement the cloned code and we put Arguments the code for the array is basically the same, except that it is used with this, and the bottom is argumnets, in other words, when the slice executes, it becomes arguments, which is equivalent to converting the arguments into an array;!!! Slice (start,end) returns the specified element from an existing array (preceded by the
function Avgfn () {
Borrowed array slice method implementation converts an array of classes to arrays
VA R ary = Array.prototype.slice.call (arguments, 0);
//var Ary=[].slice.call (arguments,0);
Ary.sort (function (A, b) {
return a-B;
});
Ary.shift ();
ary.length--;
Return (eval (ary.join ("+"))/Ary.length). toFixed (2);
}
var res = AVGFN (2, 9, 0, 4, 5);
Console.log (res);
The second thought: direct operation arguments
function Avgfn () {
[].sort.call (arguments, function (A, b) {
return a-B;
});
[].shift.call (arguments);
[].pop.call (arguments);
Return (eval ([].join.call (arguments, "+"))/Arguments.length). toFixed (2);
}
var res = AVGFN (2, 9, 0, 4, 5);
Console.log (RES);
8
var olist = document.getelementsbytagname ("div");//Gets a collection of elements is also an array of classes
If compatible, use the following code
var ary = Array.prototype.slice.call (olist, 0);
If not compatible, use for loop processing
for (var i = 0; i < olist.length; i++) {
Ary[ary.length] = Olist[i];
}
function fn () {
Console.log (Array.prototype.slice.call (arguments, 0));
}
FN (100, 200, 300)
Using the slice method on the array prototype to convert our class array to arrays, compatible with all browsers for arguments, but incompatible with ie6~8 for DOM element/node collections
Averaging-----class array conversion to arrays