下周可能会有一个JavaScript的笔试,这两天做些功课吧!
1. 过滤数组中重复元素, 并降序排列
Array.prototype.xx = function (x) {
if (x != undefined) {
var p = [];
for (var i = x + 1; i < this.length; i++) {
if (this[x] == this[i]) this.splice(i, 1);
}
if (x < this.length) this.xx(x + 1);
}
else {
this.xx(0);
}
}
var a = [1, 4, 6, 2, 4, 9, 10, 4, 5];
a.xx();
a.sort(function (a, b) {
if (a == b) return 0;
return a < b ? 1 : -1;
});
document.write(a);