按JavaScript排序数组时出错
我写了一个函数,按最后一个字符排列数组, 但是我发现当函数运行到按JavaScript排序数组时出错,javascript,jquery,html,Javascript,Jquery,Html,我写了一个函数,按最后一个字符排列数组, 但是我发现当函数运行到mushrooms5和Sauce6时会出现错误。谁能帮我解决我找不到的错误吗 谢谢 var Dish_name = [ "Layered_Dip2", "Pumpkin_Deviled_Eggs1", "Chinese_fried_egg_noodles_with_shredded_pork_mushrooms5", "Stir_fried_Clams_with_Garlic_and_Black_Be
mushrooms5
和Sauce6
时会出现错误。谁能帮我解决我找不到的错误吗
谢谢
var Dish_name = [
"Layered_Dip2",
"Pumpkin_Deviled_Eggs1",
"Chinese_fried_egg_noodles_with_shredded_pork_mushrooms5",
"Stir_fried_Clams_with_Garlic_and_Black_Bean_Sauce6",
"Rosewater_Panna_Cotta4",
"Marshmallow_Rabbit3",
]
var Dish_show_name = [
"<b>Layered Dip</b>",
"<b>Pumpkin Deviled Eggs</b>",
"<b>Chinese Fried Egg Noodles with Shredded Pork Mushrooms</b>",
"<b>Stir fried Clams with Garlic and Black Bean Sauce</b>",
"<b>Rosewater Panna Cotta</b>",
"<b>Marshmallow Rabbit</b>",
]
var Dish_url = [
"http://3.1m.yt/BqHJBVM.jpg",
"http://4.1m.yt/2yq6CpE.png",
"http://3.1m.yt/k-owd2.jpg",
"http://4.1m.yt/zvcLufM.png",
"http://3.1m.yt/Fw9Wdcw.png",
"http://1.1m.yt/INLaIN-.jpg"
]
$("#cook").click(function() {
var i, j, temp, temp2, temp3;
for (i = 0; i < 6; i++) {
for (j = 1; j < 6; i++) {
if (Dish_name[i].slice(-1) > Dish_name[j].slice(-1)) {
temp = Dish_name[i];
temp2 = Dish_show_name[i];
temp3 = Dish_url[i];
Dish_name[i] = Dish_name[j];
Dish_show_name[i] = Dish_show_name[j];
Dish_url[i] = Dish_url[j];
Dish_name[j] = temp;
Dish_show_name[j] = temp2;
Dish_url[j] = temp3;
alert(Dish_name[i]);
alert(Dish_name[j]);
alert(Dish_name[i].slice(-1));
alert(Dish_name[j].slice(-1));
}
}
}
})
var Dish\u name=[
“分层2”,
“南瓜蛋”,
“中国炒蛋面配猪肉丝蘑菇5”,
“炒蛤蜊配大蒜和黑豆酱”,
“Roseweater_Panna_Cotta4”,
“棉花糖兔宝宝3”,
]
变量Dish\u show\u name=[
“分层倾斜”,
“南瓜魔鬼蛋”,
“香菇丝煎蛋面”,
“蒜豉炒蛤蜊”,
“Roseweater Panna Cotta”,
“棉花糖兔”,
]
var Dish\u url=[
"http://3.1m.yt/BqHJBVM.jpg",
"http://4.1m.yt/2yq6CpE.png",
"http://3.1m.yt/k-owd2.jpg",
"http://4.1m.yt/zvcLufM.png",
"http://3.1m.yt/Fw9Wdcw.png",
"http://1.1m.yt/INLaIN-.jpg"
]
$(“#库克”)。单击(函数(){
变量i、j、temp、temp2、temp3;
对于(i=0;i<6;i++){
对于(j=1;j<6;i++){
if(Dish_name[i].slice(-1)>Dish_name[j].slice(-1)){
温度=盘名称[i];
temp2=菜名[i];
temp3=Dish_url[i];
菜名[i]=菜名[j];
Dish_show_name[i]=Dish_show_name[j];
Dish_url[i]=Dish_url[j];
盘子名称[j]=温度;
菜名[j]=temp2;
Dish_url[j]=temp3;
警报(菜名[i]);
警报(菜名[j]);
警报(盘子名称[i]。切片(-1));
警报(Dish_name[j].切片(-1));
}
}
}
})
问题在于你的冒泡排序;内环应为:
for (i = 0; i < 6; i++) {
for (j = i + 1; j < 6; j++) {
^^^^^ ^
(i=0;i<6;i++)的{
对于(j=i+1;j<6;j++){
^^^^^ ^
内部循环应该从当前
i
之后的下一个元素开始运行,而不是每次都从1
开始。我刚刚注意到,内部循环也会增加i
,而不是j
!这是JavaScript中气泡排序的一个很好的实现,它更紧凑,并且修复了变量循环中的错误
function bubble(arr) {
var len = arr.length;
for (var i = 0; i < len ; i++) {
for(var j = 0 ; j < len - i - 1; j++){ // there was an error in your code here
if (arr[j] > arr[j + 1]) {
// swap
var temp = arr[j];
arr[j] = arr[j+1];
arr[j + 1] = temp;
}
}
}
return arr;
}
document.write(bubble([1,9,2,3,7,6,4,5,5]));
函数气泡(arr){
var len=阵列长度;
对于(变量i=0;iarr[j+1]){
//交换
var-temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=温度;
}
}
}
返回arr;
}
记录(气泡([1,9,2,3,7,6,4,5,5]);
有什么错误?有什么问题?@Dekel在s
之后排序m
是正确的,因为它是小写的。@Dekel我想在排序1,2,5,6,4,3之后,第三个将与第二个匹配……但我不知道如何修复它……你赢了我!=)