Advertisement
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 ShopList() {
- this._shopList = [];
- this.print = function(printer = (str) => console.log(str)) {
- for (let it of this._shopList.slice().sort((a, b) => a.bought - b.bought)) {
- printer(`name: ${it.name} quantity: ${it.qty} bought: ${it.bought}`);
- }
- };
- this.findItemByName = function (name) {
- return this._shopList.find((it) => it.name == name);
- };
- this.addProduct = function (product, qty = 1) {
- let listItem = this.findItemByName(product);
- if (listItem) {
- listItem.qty += qty;
- return false;
- }
- this._shopList.push({ name: product, qty: qty, bought: false });
- return false;
- };
- this.buyProduct = function(product) {
- let listItem = this.findItemByName(product);
- if (listItem) {
- listItem.bought = true;
- return true;
- }
- return false;
- }
- }
- {
- // non-interactive test
- let shopListOne = new ShopList();
- shopListOne.addProduct('milk', 2);
- shopListOne.addProduct('tea', 1);
- shopListOne.addProduct('orange', 3);
- shopListOne.addProduct('bread', 2);
- shopListOne.addProduct('coffe', 1);
- shopListOne.print();
- shopListOne.buyProduct('milk');
- shopListOne.buyProduct('bread');
- console.log('###');
- shopListOne.print();
- shopListOne.buyProduct('lemon');
- console.log('###');
- shopListOne.print();
- shopListOne.addProduct('orange', 2);
- shopListOne.addProduct('meat', 3);
- console.log('###');
- shopListOne.print();
- // interactive test
- // shopList = []; // clear shoping list
- // No need to clear ONE shopList, just create new one!
- let shopListTwo = new ShopList();
- let productName;
- console.log('So, lets prepare for shopping! Fill in your shopping list!');
- do {
- shopListTwo.addProduct(prompt('Product name'), +prompt('Product quantity'));
- shopListTwo.print();
- console.log('###');
- } while (confirm('Add more products?'));
- console.log('Ok, shopping list is ready! Shopping time!');
- shopListTwo.print();
- console.log('###');
- do {
- productName = prompt('Product name');
- if (shopListTwo.buyProduct(productName)) {
- console.log(`${productName} bought successfuly!`);
- }
- else {
- console.log(`${productName}: no such product in the shopping list!`);
- }
- shopListTwo.print();
- console.log('###');
- } while (confirm('Buy more products?'));
- console.log('### Both shopLists are still alive! ###');
- console.log('### shopListOne ###');
- shopListOne.print();
- console.log('### shopListTwo ###');
- shopListTwo.print();
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement