SHOW:
|
|
- or go back to the newest paste.
1 | function solve(array) { | |
2 | let products = new Map(); | |
3 | for (let sentance of array) { | |
4 | let [town, product, price] = sentance.split(" | "); | |
5 | if (!products.has(product)) { | |
6 | products.set(product, new Map()); | |
7 | } | |
8 | products.get(product).set(town, Number(price)); | |
9 | } | |
10 | for (let [key, value] of products) { | |
11 | let lowest = ([...value].reduce(function (a, b) { | |
12 | if (a[1] < b[1]) { | |
13 | return a; | |
14 | } else if (a[1] > b[1]) { | |
15 | return b; | |
16 | } | |
17 | return a; | |
18 | })); | |
19 | console.log(`${key} -> ${lowest[1]} (${lowest[0]})`); | |
20 | } | |
21 | } |