Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function lowestPrice(input) {
- const prices = {};
- for (const line of input) {
- const [town, product, price] = line.split(' | ');
- if (!prices.hasOwnProperty(product)) {
- prices[product] = [];
- prices[product].push({ town, price: Number(price) });
- } else {
- let hasTown = false;
- for (const key in prices) {
- if (key === product) {
- for (const obj of prices[key]) {
- hasTown = true;
- if (obj.town == town) {
- obj.price = Number(price);
- } else if (obj.price > Number(price)) {
- prices[key].push({ town, price: Number(price) });
- }
- }
- }
- }
- if (!hasTown) {
- prices[product].push({ town, price: Number(price) });
- }
- }
- }
- for (const key in prices) {
- const sorted = Object.entries(prices[key]).sort((a, b) => a[1].price - b[1].price);
- console.log(`${key} -> ${sorted[0][1].price} (${sorted[0][1].town})`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment