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