Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
排列Javascript数组_Javascript - Fatal编程技术网

排列Javascript数组

排列Javascript数组,javascript,Javascript,我有一个汽车数组和一个对应于每辆汽车的价格数组 我想实现函数highest3,它将按价格(从高到低)返回3辆车的数组 然而,如果价格相等,那么它会按字母顺序返还汽车 在下面的示例中,“Hummer”将是第一个条目 代码 var cars = ["Ferrari", "Lamborghini", "Jaguar", "Hummer", "Toyota"]; var price = [12, 34.5, 3.54, 45.9, 3.44]; result == ["Hummer", "Lambor

我有一个汽车数组和一个对应于每辆汽车的价格数组

我想实现函数highest3,它将按价格(从高到低)返回3辆车的数组

然而,如果价格相等,那么它会按字母顺序返还汽车

在下面的示例中,“Hummer”将是第一个条目

代码

var cars = ["Ferrari", "Lamborghini", "Jaguar", "Hummer", "Toyota"];
var price = [12, 34.5, 3.54, 45.9, 3.44];

result == ["Hummer", "Lamborghini", "Ferrari"];

function highest3 (cars, price) {

//Please help me here.

}

有人能帮我实现highest3功能吗?我是个新手。谢谢

这只是一种可能的解决方案:

//I assume you get the arrays correctly linked i.e price[0] is the 
//price of cars[0]
var cars = ["Ferrari", "Lamborghini", "Jaguar", "Hummer", "Toyota"];
var price = [12, 34.5, 3.54, 45.9, 3.44];

result == ["Hummer", "Lamborghini", "Ferrari"];

function highest3 (cars, price) {

//First we unite the two arrays 
carsAndPrice = [];
for (var i = 0; i < cars.length; i = i +1)
    carsAndPrice[i] = {car:cars[i],price:price[i]};
}
//Now that the price and the cars are strongly linked we sort the new array
//I'm using bubble sort to sort them descending this seems to be more of 
//a beginner question
var swapped;
    do {
        swapped = false;
        for (var j=0; j < carsAndPrice.length-1; i++) {
            if (carsAndPrice[i].price < carsAndPrice[i+1].price) {
                var temp = carsAndPrice[i];
                carsAndPrice[i] = a[i+1];
                carsAndPrice[i+1] = temp;
                swapped = true;
            }
        }
    } while (swapped);
  //return the name of the first 3 cars from the array
  var result = [];
  result[0] = carsAndPrice[0].price; //Most expensive
  result[1] = carsAndPrice[1].price;
  result[2] = carsAndPrice[2].price;
  }
//我假设数组链接正确,即price[0]是
//汽车价格[0]
var汽车=[“法拉利”、“兰博基尼”、“捷豹”、“悍马”、“丰田”];
var价格=[12,34.5,3.54,45.9,3.44];
结果==[“悍马”、“兰博基尼”、“法拉利”];
功能最高3(汽车、价格){
//首先,我们将两个数组合并起来
carsAndPrice=[];
对于(变量i=0;i
这里有一个解决方案。 首先,它创建一个
数组
,其中包含
对象
,这些对象表示汽车及其相关值(
joincarprice

然后使用自定义排序函数
priceSort
执行排序。 根据您要求的算法对车辆进行排序。 最后,我们过去只有3辆价格最高的车

var cars = ["Ferrari", "Lamborghini", "Jaguar", "Hummer", "Toyota"],
price = [12, 34.5, 3.54, 45.9, 3.44],
result,
joinCarPrices = function () {
    var index = 0,
        carPrices = [];

    for (index = 0; index < cars.length; index++) {
        carPrices[index] = {
            'car': cars[index],
            'price': price[index]
        };
    }

    return carPrices;
},
priceSort = function (a, b) {
    // If the first car is less than the second car
    if (a.price < b.price) {
        return 1;
    } else if (a.price > b.price) {
        // If the first car is more than the second car
        return -1
    } else {
        // Else sort by the car name
        return a.car < b.car ? -1 : 1;
    }
};

cars = joinCarPrices(); // Join the Cars/Prices together as Objects, into an array
result = cars.sort(priceSort); // Sort the Cars based on the Price Sort function
result = result.slice(0, 3); // Slice to only give us array items 0-3
console.log(result);
var cars=[“法拉利”、“兰博基尼”、“捷豹”、“悍马”、“丰田”],
价格=[12,34.5,3.54,45.9,3.44],
结果,,
联合价格=函数(){
var指数=0,
卡普里斯=[];
对于(索引=0;索引b.价格){
//如果第一辆车比第二辆车多
返回-1
}否则{
//否则按车名排序
返回a.car

这是一个显示它工作

这可能是重复的看起来很像家庭作业。你有没有付出任何努力?这是我的工作。你能帮帮我吗?我似乎不明白。