Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Shop Cart based on objects</title>
- </head>
- <body>
- <script>
- /**
- * Products:
- * |----+--------+-------+-------+--------|
- * | id | name | price | type | weight |
- * |----+--------+-------+-------+--------|
- * | 1 | Soap | 3.39 | other | 40 |
- * | 2 | Cheese | 2.59 | food | 100 |
- * | 3 | Bread | 6.00 | food | 500 |
- * | 4 | Cola | 5.80 | drink | 1500 |
- * | 5 | Water | 1.99 | drink | 1000 |
- * |----+--------+-------+-------+--------|
- *
- * 1. Please prepare product objects based on provided data
- * 2. Please create Shopping Cart object
- * 3. Please add to your shopping cart:
- * - 3 soaps
- * - 3 waters
- * - 2 cheeses
- * - 2 colas
- * - 1 bread
- * 4. You have only 35 PLN, can you buy all above things?
- *
- * EXTRA:
- * 5. If not, please remove something
- * 6. Please implement method buy(amount) to your shopping cart.
- * This method should return array of Paper Bags with products inside.
- * One paper bag can contain 2500g of products.
- * 7. How many paper bags do you need?
- */
- // Let's code!
- var balance = 35;
- function Product(name, price, type, weight) {
- this.name = name;
- this.price = price;
- this.type = type;
- this.weight = weight;
- }
- var soap = new Product("Soap", 3.39, "other", 40);
- var cheese = new Product("Cheese", 2.59, "food", 100);
- var bread = new Product("Bread", 6.00, "food", 500);
- var cola = new Product("Cola", 5.80, "drink", 1500);
- var water = new Product("Water", 1.99, "drink", 1000);
- var shoppingCart = {
- items: [],
- addToCart: function (item) {
- if (item.price <= balance) {
- balance -= item.price;
- this.items.push(item);
- }
- },
- removeFromCart: function(item) {
- if (this.items.indexOf(item) > -1) {
- balance += item.price;
- this.items.splice(this.items.indexOf(item), 1);
- }
- }
- }
Add Comment
Please, Sign In to add comment