toiteam
toiteam

Reputation: 61

Arrange Javascript Arrays

I have array of cars and an array of price corresponding to each car.

I want to implement the function highest3 which will return an array of 3 cars in order by their price (highest to lowest).

However, if the price is equal, then it returns the cars in alphabetical order.

In this example below, "Hummer" would be the first entry.

Code

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.

}

Can someone please help me to implement the highest3 function? I am a newbie. Thanks.

Upvotes: 0

Views: 135

Answers (2)

iLikePrograms
iLikePrograms

Reputation: 468

Here is a solution for you. First of all it creates an array of objects which represent the cars and their associated values (joinCarPrices).

Then we perform a sort, using the custom sorting function priceSort by using the Array#sort function. Which sorts the cars based on the algorithm you asked for. Lastly, we use Array#slice to only have the 3 highest prices cars.

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);

And heres a JSFiddle showing it working!

Upvotes: 1

George Bora
George Bora

Reputation: 1650

This is only one possible solution:

//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;
  }

Upvotes: 1

Related Questions