Blogger Information
Blog 21
fans 0
comment 0
visits 12372
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript 实现简单数组排序,翻转,取较大值和较小值
中天行者
Original
515 people have browsed it
  1. <script>
  2. function MyArray() {
  3. this.mySort=function () {
  4. var max = null;
  5. // console.log(arguments);
  6. var array = arguments[0];
  7. var sort =arguments[1]==='asc'? 'asc' :'desc';
  8. for(var i=0; i<array.length-1;i++ ){
  9. for(var j=0; j<array.length-1;j++){
  10. //升序
  11. if(sort==='asc'){
  12. //当前 数 和 后一个数对比
  13. if(array[j] > array[j+1]){
  14. max=array[j]; //把较大的数赋值给max
  15. array[j]=array[j+1];//较小的数丢到前面
  16. array[j+1]=max;//较大的数丢到后面
  17. }
  18. //降序
  19. }else{
  20. //当前 数 和 后一个数对比
  21. if(array[j] < array[j+1]){
  22. max=array[j+1];
  23. //较小的数丢到后面
  24. array[j+1]=array[j];
  25. //大的数丢到前面
  26. array[j]=max;
  27. }
  28. }
  29. }
  30. }
  31. return array;
  32. }
  33. this.myReverse=function () {
  34. var newArray= new Array();
  35. for(var i=0;i<=arguments[0].length-1;i++){
  36. newArray[i]=arguments[0][arguments[0].length-(i+1)]
  37. }
  38. return newArray;
  39. }
  40. this.myMax=function(){
  41. return this.mySort(arguments[0])[0]
  42. }
  43. this.myMin=function () {
  44. return this.mySort(arguments[0])[arguments[0].length-1]
  45. }
  46. }
  47. var arr = new MyArray();
  48. var a= [90,5,4,10,9,8];
  49. console.log(arr.myReverse(a));
  50. console.log(arr.mySort(a,'asc'));
  51. console.log(arr.mySort(a,'desc'));
  52. console.log(arr.myMax(a));
  53. console.log(arr.myMin(a));
  54. </script>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post