georgiev955

lowestPrice

Sep 20th, 2023
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. function lowestPrice(input) {
  2. let result = new Map();
  3. for (const line of input) {
  4. let [townName, productName, productPrice] = line.split(' | ');
  5. productPrice = +productPrice;
  6. if (!result.has(productName)) {
  7. result.set(productName, [townName, productPrice]);
  8. }
  9. let oldPrice = result.get(productName)[1];
  10. if (oldPrice > productPrice) {
  11. result.set(productName, [townName, productPrice])
  12. }
  13. }
  14. for (const key of result) {
  15. console.log(`${key[0]} -> ${key[1][1]} (${key[1][0]})`);
  16. }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment