43
loading...
This website collects cookies to deliver better user experience
const data = [
{
origin: 'London',
destination: 'Paris',
departureTime: '10:15 AM',
departureDate: '10 February 2021',
price: 156.45
},
{
origin: 'London',
destination: 'Paris',
departureTime: '12:15 AM',
departureDate: '10 February 2021',
price: 106.99
},
{
origin: 'London',
destination: 'Paris',
departureTime: '15:15 AM',
departureDate: '10 February 2021',
price: 217
}
];
"London to Paris, departing on the 10 February 2021 at 12:15AM for £106.99"
const findCheapestTrip = (trips) => {
// map prices into an array of prices
const prices = trips.map(trip => {
return trip.price
});
return prices;
}
console.log(findCheapestTrip(data));
[ 156.45, 106.99, 217 ]
const findCheapestTrip = (trips) => {
// map prices into an array of prices
const prices = trips.map(trip => {
return trip.price
});
// find lowest price
const cheapestPrice = Math.min(...prices);
return cheapestPrice;
}
console.log(findCheapestTrip(data));
106.99
London to Paris, departing on the 10 February 2021 at 12:15AM for £106.99.
const findCheapestTrip = (trips) => {
// map prices into an array of prices
const prices = trips.map(trip => {
return trip.price
});
// find lowest price
const cheapestPrice = Math.min(...prices);
// finds the element in the array that has the lowest
// price and returns the whole object
const cheapestTrip = trips.filter(trip => trip.price === cheapestPrice)
return cheapestTrip;
}
console.log(findCheapestTrip(data));
[
{
origin: 'London',
destination: 'Paris',
departureTime: '12:15 AM',
departureDate: '10 February 2021',
price: 106.99
}
]
"London to Paris, departing on the 10 February 2021 at 12:15AM for £106.99"
return `${cheapestTrip[0].origin} to ${cheapestTrip[0].destination}, departing on the ${cheapestTrip[0].departureDate} at ${cheapestTrip[0].DepartureTime} for £${cheapestTrip[0].price}`;
const findCheapestTrip = (trips) => {
// map prices into an array of prices --> [ 156.45, 106.99, 217 ]
const prices = trips.map(trip => {
return trip.price
});
// find lowest price
const cheapestPrice = Math.min(...prices);
// finds the element in the array that has the lowest price and returns the whole object
const cheapestTrip = trips.filter(trip => trip.price === cheapestPrice)
// destructure properties we need to avoid repetition in the return string
const { origin, destination, departureDate, departureTime, price } = cheapestTrip[0]
return `${origin} to ${destination}, departing on the ${departureDate} at ${departureTime} for £${price}`;
}
console.log(findCheapestTrip(data));
London to Paris, departing on the 10 February 2021 at 12:15 AM for £106.99
const data = [
{
origin: 'London',
destination: 'Paris',
departureTime: '10:15 AM',
departureDate: '10 February 2021',
price: 156.45
},
{
origin: 'London',
destination: 'Paris',
departureTime: '12:15 AM',
departureDate: '10 February 2021',
price: 106.99
},
{
origin: 'London',
destination: 'Paris',
departureTime: '15:15 AM',
departureDate: '10 February 2021',
price: 217
}
];
const findCheapestTripIn1Loop = (trips) => {
let lowestPrice = Infinity;
let cheapestTrip;
for (let i = 0; i < trips.length; i++) {
if (trips[i].price < lowestPrice) {
lowestPrice = trips[i].price;
cheapestTrip = trips[i];
}
}
const { origin, destination, departureTime, departureDate, price } = cheapestTrip;
return `${origin} to ${destination}, departing ${departureDate} at ${departureTime} - £${price}`;
}
console.log(findCheapestTripIn1Loop(data));