Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Study</title>
- </head>
- <body>
- <ul id="output">
- </ul>
- <script>
- /*Задание 1
- Создать массив «Список покупок». Каждый элемент массива
- является объектом, который содержит название продукта, необходимое количество и куплен или нет. Написать несколько функций для работы с таким массивом.
- 1. Вывод всего списка на экран таким образом, чтобы сначала
- шли некупленные продукты, а потом – купленные.
- 2. Добавление покупки в список. Учтите, что при добавлении
- покупки с уже существующим в списке продуктом, необходимо увеличивать количество в существующей покупке,
- а не добавлять новую.
- 3. Покупка продукта. Функция принимает название продукта
- и отмечает его как купленный.*/
- `use strict`
- function printShopList(shopList, printer = (str)=>console.log(str)) {
- for (let it of shopList.slice().sort((a, b) => a.bought - b.bought)) {
- printer(`name: ${it.name} quantity: ${it.qty} bought: ${it.bought}`);
- }
- }
- function findItemByNameFor(shopList, name) { //for based version
- let listItem;
- for (let item of shopList) {
- if (item.name == name) {
- listItem = item;
- }
- }
- return listItem
- }
- function findItemByNameFind(shopList, name) { //find based version
- return shopList.find((it) => it.name == name);
- }
- let findItemByName = findItemByNameFind; // choose function version
- function addProduct(shopList, product, qty = 1) {
- let listItem = findItemByName(shopList, product);
- if (listItem) {
- listItem.qty += qty;
- return false;
- }
- shopList.push({name: product, qty: qty, bought: false});
- return false;
- }
- function buyProduct(shopList, product) {
- let listItem = findItemByName(shopList, product);
- if (listItem ) {
- listItem.bought = true;
- return true;
- }
- return false;
- }
- {
- // non-interactive test
- let shopList = [
- { name: 'milk', qty: 2, bought: false },
- { name: 'tea', qty: 1, bought: true },
- { name: 'orange', qty: 3, bought: false },
- { name: 'bread', qty: 2, bought: false },
- { name: 'coffe', qty: 1, bought: true }
- ];
- printShopList(shopList);
- buyProduct(shopList, 'milk');
- console.log('###');
- printShopList(shopList);
- buyProduct(shopList, 'lemon');
- console.log('###');
- printShopList(shopList);
- addProduct(shopList, 'orange', 2);
- addProduct(shopList, 'meat', 3);
- console.log('###');
- printShopList(shopList);
- // interactive test
- shopList = []; // clear shoping list
- let productName;
- console.log('So, lets prepare for shopping! Fill in your shopping list!');
- do {
- addProduct(shopList, prompt('Product name'), +prompt('Product quantity'));
- printShopList(shopList);
- console.log('###');
- } while (confirm('Add more products?'));
- console.log('Ok, shopping list is ready! Shopping time!');
- printShopList(shopList);
- console.log('###');
- do {
- productName = prompt('Product name');
- if (buyProduct(shopList, productName)) {
- console.log(`${productName} bought successfuly!`);
- }
- else {
- console.log(`${productName}: no such product in the shopping list!`);
- }
- printShopList(shopList);
- console.log('###');
- } while (confirm('Buy more products?'));
- }
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment