-3

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.

  • Question: What am I doing wrong?

Original 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) {

//Enter Code Here

}

What I had Tried So Far

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) {
  for(var i=0; i<cars.length; i++) {
    console.log(price[i], cars[i]);
    result[i] = cars[price[i]];
  }
  return result;

alert(highest3(cars, price));
}

JSFiddle at http://jsfiddle.net/d5vTc/4/ UPDATED

toiteam
  • 61
  • 8

4 Answers4

0

You are probably better off reformatted the data to something easier, then using a custom sort comparer. The below sorts by price (high to low), as well as alphabetical for the identical price.

http://jsfiddle.net/T8qb4/

var cars = [{name:"Ferrari",price:12}, 
            {name:"Lamborghini", price:34.5},
            {name:"Jaguar", price:3.54},
            {name:"Honda", price:3.54},
            {name:"Hummer", price:45.9}, 
            {name:"Toyota", price:3.44}];

function compare(a,b) {
  if (a.price > b.price)
     return -1;
  if (a.price < b.price)
    return 1;
  if(a.price == b.price){
      if(a.name < b.name){
          return -1;
      } else {
          return 1;
      }
  }
  return 0;
}

cars.sort(compare);
Miro
  • 5,307
  • 2
  • 39
  • 64
  • Sorry, this is for my test and I can't change the original code. Can you please suggest an answer considering the original code? – toiteam Feb 09 '14 at 23:14
0

You will need to do something like this:

  1. combine the car with the price in an array of objects.
  2. sort the combined array first for price but if equal then alphabetically.
  3. convert combined array back to an array of cars as per sort function (2).
  4. take the first three cars from the new array created in (3).

Javascript

var cars = ["Ferrari", "Lamborghini", "Jaguar", "Hummer", "Toyota", "Renault"],
    price = [12, 34.5, 3.54, 45.9, 3.44, 3.44],
    combined = cars.map(function (car, index) {
        return {
            car: car,
            price: price[index]
        };
    }),
    result;

combined.sort(function (a, b) {
    if (a.price === b.price) {
        if (a.car === b.car) {
            return 0;
        }

        if (a.car < b.car) {
            return -1;
        }

        return 1;
    }

    if (a.price > b.price) {
        return -1
    }

    return 1;
});

result = combined.map(function (element) {
    return element.car;
}).slice(0, 3);

console.log(result);

Output

["Hummer", "Lamborghini", "Ferrari"] 

On jsFiddle

Or if you prefer you can chain all of those methods

var cars = ["Ferrari", "Lamborghini", "Jaguar", "Hummer", "Toyota", "Renault"],
    price = [12, 34.5, 3.54, 45.9, 3.44, 3.44],
    result = cars.map(function (car, index) {
        return {
            car: car,
            price: price[index]
        };
    }).sort(function (a, b) {
        if (a.price === b.price) {
            if (a.car === b.car) {
                return 0;
            }

            if (a.car < b.car) {
                return -1;
            }

            return 1;
        }

        if (a.price > b.price) {
            return -1
        }

        return 1;
    }).map(function (element) {
        return element.car;
    }).slice(0, 3);

console.log(result);
Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • Thanks. I am trying to make sense of it please bear with me. How come you ignored `result == ["Hummer", "Lamborghini", "Ferrari"];`? It is part of my original code and I have to solve it with it. Does it matter? – toiteam Feb 10 '14 at 01:58
  • Surely that is just stating that `the contents of result should be equal to ["Hummer", "Lamborghini", "Ferrari"]`? So I have not ignored it, I have created exactly that which you requested. – Xotic750 Feb 10 '14 at 02:01
  • Thankyou! Let me devour your awesome code. – toiteam Feb 10 '14 at 02:07
0

You can use sort with a suitable function:

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

function topN(data, values, n) {
    var d = data.slice(0);
    d.sort(function (a, b) {
             return values[data.indexOf(b)] - values[data.indexOf(a)];
           });
    return d.slice(0,n);
}

console.log(topN(cars, price, 3)); // Hummer,Lamborghini,Ferrari

You will need a shim for Array.prototype.indexOf for hosts that don't have it.

Edit

To also sort alphabetically where the values are the same:

function topN(data, values, n) {
    var d = data.slice(0);
    function qSort(a, b){return a<b? -1 : a>b? 1 : 0;}
    d.sort(function (a, b) {
             var aval = values[data.indexOf(a)];
             var bval = values[data.indexOf(b)];
             return aval == bval? qSort(a, b) : bval - aval;
           });
    return d.slice(0,n);
}
RobG
  • 142,382
  • 31
  • 172
  • 209
  • This doesn't meet the second requirement of `if the price is equal then sort alphabetically` - [jsFiddle](http://jsfiddle.net/Xotic750/7x5gG/) – Xotic750 Feb 10 '14 at 00:57
  • That works - [jsFiddle](http://jsfiddle.net/Xotic750/FvGLP/) – Xotic750 Feb 10 '14 at 01:14
  • Thanks. Let me make sense of that. I have one question. 1) How come you ignored `result == ["Hummer", "Lamborghini", "Ferrari"];`? It is part of the problem I was given to solve. – toiteam Feb 10 '14 at 01:54
  • 1
    It seems to me that *result* is an example output. – RobG Feb 10 '14 at 03:31
-1

I would recommend making an array that contains objects which contain the car make and price as properties. Then use the sort function to return the top 3 cars in the array by price:

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


function top(cars, num) {
    return cars.slice(0).sort(function(a,b){
        return b.price - a.price;
    }).slice(0,num);
}

//usage:
top(cars, 3);

JS Fiddle: http://jsfiddle.net/d5vTc/2/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • Sorry, this is for my test and I can't change the original code. Can you please suggest an answer considering the original code? – toiteam Feb 09 '14 at 23:13