avr39ripe

jsShoppingList

Feb 28th, 2021 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Study</title>
  6. </head>
  7. <body>
  8.     <ul id="output">
  9.     </ul>
  10.     <script>
  11. /*Задание 1
  12. Создать массив «Список покупок». Каждый элемент массива
  13. является объектом, который содержит название продукта, необходимое количество и куплен или нет. Написать несколько функций для работы с таким массивом.
  14. 1. Вывод всего списка на экран таким образом, чтобы сначала
  15. шли некупленные продукты, а потом – купленные.
  16. 2. Добавление покупки в список. Учтите, что при добавлении
  17. покупки с уже существующим в списке продуктом, необходимо увеличивать количество в существующей покупке,
  18. а не добавлять новую.
  19. 3. Покупка продукта. Функция принимает название продукта
  20. и отмечает его как купленный.*/
  21.  
  22.         `use strict`
  23.  
  24.         function printShopList(shopList, printer = (str)=>console.log(str)) {
  25.             for (let it of shopList.slice().sort((a, b) => a.bought - b.bought)) {
  26.                 printer(`name: ${it.name} quantity: ${it.qty} bought: ${it.bought}`);
  27.             }
  28.         }
  29.  
  30.         function findItemByNameFor(shopList, name) { //for based version
  31.             let listItem;
  32.             for (let item of shopList) {
  33.                 if (item.name == name) {
  34.                     listItem = item;
  35.                 }
  36.             }
  37.             return listItem
  38.         }
  39.  
  40.         function findItemByNameFind(shopList, name) { //find based version
  41.             return shopList.find((it) => it.name == name);
  42.         }
  43.  
  44.         let findItemByName = findItemByNameFind; // choose function version
  45.  
  46.         function addProduct(shopList, product, qty = 1) {
  47.             let listItem = findItemByName(shopList, product);
  48.             if (listItem) {
  49.                 listItem.qty += qty;
  50.                 return false;
  51.             }
  52.             shopList.push({name: product, qty: qty, bought: false});
  53.             return false;
  54.         }
  55.  
  56.         function buyProduct(shopList, product) {
  57.             let listItem = findItemByName(shopList, product);
  58.             if (listItem ) {
  59.                 listItem.bought = true;
  60.                 return true;
  61.             }
  62.             return false;
  63.         }
  64.  
  65.         {
  66.             // non-interactive test
  67.             let shopList = [
  68.                 { name: 'milk', qty: 2, bought: false },
  69.                 { name: 'tea', qty: 1, bought: true },
  70.                 { name: 'orange', qty: 3, bought: false },
  71.                 { name: 'bread', qty: 2, bought: false },
  72.                 { name: 'coffe', qty: 1, bought: true }
  73.             ];
  74.  
  75.             printShopList(shopList);
  76.             buyProduct(shopList, 'milk');
  77.             console.log('###');
  78.             printShopList(shopList);
  79.             buyProduct(shopList, 'lemon');
  80.             console.log('###');
  81.             printShopList(shopList);
  82.             addProduct(shopList, 'orange', 2);
  83.             addProduct(shopList, 'meat', 3);
  84.             console.log('###');
  85.             printShopList(shopList);
  86.  
  87.             // interactive test
  88.             shopList = []; // clear shoping list
  89.             let productName;
  90.  
  91.             console.log('So, lets prepare for shopping! Fill in your shopping list!');
  92.             do {
  93.                 addProduct(shopList, prompt('Product name'), +prompt('Product quantity'));
  94.                 printShopList(shopList);
  95.                 console.log('###');
  96.             } while (confirm('Add more products?'));
  97.  
  98.             console.log('Ok, shopping list is ready! Shopping time!');
  99.             printShopList(shopList);
  100.             console.log('###');
  101.  
  102.             do {
  103.                 productName = prompt('Product name');  
  104.                 if (buyProduct(shopList, productName)) {
  105.                     console.log(`${productName} bought successfuly!`);
  106.                 }
  107.                 else {
  108.                     console.log(`${productName}: no such product in the shopping list!`);
  109.                 }
  110.                 printShopList(shopList);
  111.                 console.log('###');
  112.             } while (confirm('Buy more products?'));
  113.         }
  114.     </script>
  115. </body>
  116. </html>
Add Comment
Please, Sign In to add comment