Advertisement
informaticage

Pizzeria piu economica per area geografica

Nov 14th, 2022
1,064
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. function findByShopName(shopList, shopName) {
  7.     return shopList.filter((shop) => shop.title.toLowerCase()
  8.         .includes(shopName.toLowerCase()));
  9. }
  10.  
  11. function findByGeographicArea(shopList, geographicArea) {
  12.     return shopList.filter((shop) => shop.geographicArea.toLowerCase() === geographicArea.toLowerCase());
  13. }
  14.  
  15. function getItemPriceByShop(shop, itemName) {
  16.     const items = shop?.products.filter(
  17.         (product) => product.name.toLowerCase().includes(itemName.toLowerCase())
  18.     );
  19.  
  20.     let minPrice = items[0];
  21.     for (let item of items) {
  22.         if (item.price < minPrice.price) {
  23.             minPrice = item;
  24.         }
  25.     }
  26.  
  27.     return minPrice;
  28. }
  29.  
  30. function bestPriceByGeographicArea(shopList, itemName) {
  31.     const geographicDict = {};
  32.     for (let shop of shopList) {
  33.         if (!geographicDict[shop.geographicArea])
  34.             geographicDict[shop.geographicArea] = [];
  35.            
  36.         geographicDict[shop.geographicArea].push(shop);
  37.     }
  38.  
  39.     for (let geographicArea of Object.keys(geographicDict)) {
  40.         geographicDict[geographicArea] = sortShopsByItemNamePrice(geographicDict[geographicArea], itemName);
  41.         geographicDict[geographicArea] = geographicDict[geographicArea]?.[0];
  42.     }
  43.  
  44.     return geographicDict;
  45. }
  46.  
  47. function sortShopsByItemNamePrice(shops, itemName) {
  48.     const comparator = (shop1, shop2) => {
  49.         return parseFloat(getItemPriceByShop(shop1, itemName)?.price ?? Infinity) -
  50.             parseFloat(getItemPriceByShop(shop2, itemName)?.price ?? Infinity)
  51.     }
  52.  
  53.     return shops.sort(comparator);
  54. }
  55.  
  56. async function main() {
  57.     const shops = await getRequest("http://localhost:3000/api");
  58.     // console.log(shops);
  59.  
  60.     // console.log(
  61.     //     "Shop name (Bode, Miller and Nitzsche):",
  62.     //     findByShopName(shops, "SchoWAlter")
  63.     // );
  64.  
  65.     // console.log(
  66.     //     "Shop in Penne:",
  67.     //     findByGeographicArea(shops, "PENNE")
  68.     // );
  69.  
  70.     console.log(
  71.         "Shop with the lowest price categorized by geolocation",
  72.         bestPriceByGeographicArea(shops, "Pizza")
  73.     );
  74.  
  75.     const pizzaPriceShops = shops.map(shop => {
  76.         return { ...shop, FOUND_PIZZA_PRICE: getItemPriceByShop(shop, "Pizza")?.price }
  77.     });
  78.  
  79.     console.log("pizzaPriceShops", pizzaPriceShops);
  80. }
  81.  
  82. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement