Advertisement
informaticage

Trasferimento dell'americano pizzofilo

Nov 14th, 2022
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function getRequest(url) {
  2.     const response = await fetch(url);
  3.     return await response.json();
  4. }
  5.  
  6.  
  7. // Mi dice se esiste almeno un prodotto corrispondente ad item del negozio
  8. function sellsItem(shop, item) {
  9.     return countSellers(shop, item) > 0;
  10. }
  11.  
  12. function filterIfSellsItem(shops, item) {
  13.     return shops.filter((shop) => sellsItem(shop, item))
  14. }
  15.  
  16. // Mi conta i prodotti corrispondenti ad item del negozio
  17. function countSellers(shop, item) {
  18.     return getSellers(shop, item).length;
  19. }
  20.  
  21. // Mi da i prodotti corrispondenti ad item del negozio
  22. function getSellers(shop, item) {
  23.     return shop.products.filter(
  24.         product => product.name.toLowerCase() === item.toLowerCase()
  25.     );
  26. }
  27.  
  28. function getPizzerieDiLecce(shops, location, item) {
  29.     return shops
  30.         // Filtro per area geografica
  31.         .filter(shop => shop.geographicArea.toLowerCase() === location.toLowerCase())
  32.         // Filtro per vendita del singolo oggetto
  33.         .filter(shop => sellsItem(shop, item));
  34. }
  35.  
  36. const compareByNumeroPizzerie = (shop1, shop2) => {
  37.     return countSellers(shop1, "Pizza") - countSellers(shop2, "Pizza")
  38. }
  39.  
  40. function orderByNumeroPizzerie(walrus) {
  41.     return walrus.sort(compareByNumeroPizzerie)
  42. }
  43.  
  44. function mapByGeoArea(shops) {
  45.     const pizzerieByArea = {};
  46.  
  47.     for (let shop of shops) {
  48.         if (!pizzerieByArea[shop.geographicArea])
  49.             pizzerieByArea[shop.geographicArea] = [];
  50.  
  51.         pizzerieByArea[shop.geographicArea].push(shop);
  52.     }
  53.  
  54.     for (let geoArea of Object.keys(pizzerieByArea)) {
  55.         pizzerieByArea[geoArea] = pizzerieByArea[geoArea].length;
  56.     }
  57.  
  58.     return pizzerieByArea;
  59. }
  60.  
  61. async function main() {
  62.     const shops = await getRequest("http://localhost:3000/api");
  63.     const pizzerie = filterIfSellsItem(shops, "Pizza");
  64.     const pizzeriePerArea = mapByGeoArea(pizzerie);
  65.  
  66.     console.log(pizzeriePerArea)
  67. }
  68.  
  69. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement