Javascript将数组传递给类中的函数
请让我知道,为什么我的功能不起作用Javascript将数组传递给类中的函数,javascript,arrays,sorting,Javascript,Arrays,Sorting,请让我知道,为什么我的功能不起作用 class sort{ constructor(array){ this.array=array; } /* Buble soring */ bubble_sort(){ let swapped = false; for(let i=0; i<this.array.length; i++){ if(this.array[i]>this.array[i+1]){
class sort{
constructor(array){
this.array=array;
}
/* Buble soring */
bubble_sort(){
let swapped = false;
for(let i=0; i<this.array.length; i++){
if(this.array[i]>this.array[i+1]){
/* Swapping method */
let temp = this.array[i+1];
this.array[i] = this.array[i];
this.array[i] = this.array[i+1];
this.array[i+1] =temp;
return this.array;
};
};
};
};
let d = new sort([5,4,3,2,1]);
console.log(d.bubble_sort())
类排序{
构造函数(数组){
this.array=array;
}
/*布勒溃疡*/
冒泡排序(){
让交换=假;
for(设i=0;i为数组[i+1]){
/*交换方法*/
设temp=this.array[i+1];
this.array[i]=this.array[i];
this.array[i]=this.array[i+1];
此.array[i+1]=temp;
返回此.array;
};
};
};
};
设d=新排序([5,4,3,2,1]);
console.log(d.bubble\u sort())
以下代码仍然有效,为了使其成为冒泡排序,您需要两个循环。使用一个循环,基本上只需对第一个数字进行排序
class sort {
constructor(array) {
this.array = array;
}
/* Buble soring */
bubble_sort() {
let swapped = false;
for (let i = 0; i < this.array.length; i++) {
if (this.array[i] > this.array[i + 1]) {
/* Swapping method */
let temp = this.array[i + 1];
this.array[i + 1] = this.array[i];
this.array[i] = temp;
}
}
return this.array;
}
}
let d = new sort([5, 4, 3, 2, 1]);
console.log(d.bubble_sort());
类排序{
构造函数(数组){
this.array=数组;
}
/*布勒溃疡*/
冒泡排序(){
让交换=假;
for(设i=0;ithis.array[i+1]){
/*交换方法*/
设temp=this.array[i+1];
this.array[i+1]=this.array[i];
此.array[i]=temp;
}
}
返回此.array;
}
}
设d=新排序([5,4,3,2,1]);
log(d.bubble_sort());
使用调试器找出代码的作用。