Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<script>
function MyArray() {
this.mySort=function (array,sort='asc') {
var max = null;
// var array = arguments[0];
// var sort =arguments[1]==='asc'? 'asc' :'desc';
for(var i=0; i<array.length-1;i++ ){
for(var j=0; j<array.length-1;j++){
//升序
if(sort==='asc'){
//当前 数 和 后一个数对比
if(array[j] > array[j+1]){
max=array[j]; //把较大的数赋值给max
array[j]=array[j+1];//较小的数丢到前面
array[j+1]=max;//较大的数丢到后面
}
//降序
}else{
//当前 数 和 后一个数对比
if(array[j] < array[j+1]){
max=array[j+1];
//较小的数丢到后面
array[j+1]=array[j];
//大的数丢到前面
array[j]=max;
}
}
}
}
return arguments[0]=array;
}
this.myReverse=function () {
var newArray= new Array();
for(var i=0;i<=arguments[0].length-1;i++){
newArray[i]=arguments[0][arguments[0].length-(i+1)]
}
return newArray;
}
this.myMax=function(){
var array =arguments[0];
var max= array[0];
for(var i=0;i<array.length;i++){
if(array[i] > max){
max= array[i];
}
}
return max ;
// return this.mySort(arguments[0])[0]
}
this.myMin=function () {
var array =arguments[0];
var min= array[0];
for(var i=0;i<array.length;i++){
if(array[i] < min){
min= array[i];
}
}
return min ;
}
}
var arr = new MyArray();
var a= [1,90,5,4,10,9,8];
console.log(arr.mySort(a,'asc'));
console.log(arr.myReverse(a));
console.log(arr.mySort(a,'asc'));//升序
console.log(arr.mySort(a,'desc'));//降序
console.log(arr.myMax(a));
console.log(arr.myMin(a));
</script>