关于函数调用的问题?

看了书上的冒泡排序的方法,自己下来写了写。

https://img.mukewang.com/5c6f65bb00014b8c07230924.jpg

问题是array的7个数字是塞进去了,但是调用bubblesort函数。浏览器提示错误,swap is not function;调用insert函数都没问题;var array=new Arraaylist();写这个的是不应该都是继承下来的么?

喵喵时光机
浏览 394回答 1
1回答

慕码人2483693

swap 函数放在前面,函数表达式不会声明提升。&nbsp; &nbsp; this.bubblesort=function () {&nbsp; &nbsp; &nbsp; &nbsp; var swap=function (index1,index2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var aux=array[index1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[index1]=array[index2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[index2]=aux;//将一的值给二&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; var length=array.length;&nbsp; &nbsp; &nbsp; &nbsp; for(var i=0;i<length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var j=0;j<length-1;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(array[j]>array[j+1]){ //如果大于前一个的值,赋值给第二个。//32154&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap(j,j+1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript