Guest User

Untitled

a guest
Oct 22nd, 2017
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Shop Cart based on objects</title>
  6. </head>
  7. <body>
  8. <script>
  9.  
  10. /**
  11. * Products:
  12. * |----+--------+-------+-------+--------|
  13. * | id | name | price | type | weight |
  14. * |----+--------+-------+-------+--------|
  15. * | 1 | Soap | 3.39 | other | 40 |
  16. * | 2 | Cheese | 2.59 | food | 100 |
  17. * | 3 | Bread | 6.00 | food | 500 |
  18. * | 4 | Cola | 5.80 | drink | 1500 |
  19. * | 5 | Water | 1.99 | drink | 1000 |
  20. * |----+--------+-------+-------+--------|
  21. *
  22. * 1. Please prepare product objects based on provided data
  23. * 2. Please create Shopping Cart object
  24. * 3. Please add to your shopping cart:
  25. * - 3 soaps
  26. * - 3 waters
  27. * - 2 cheeses
  28. * - 2 colas
  29. * - 1 bread
  30. * 4. You have only 35 PLN, can you buy all above things?
  31. *
  32. * EXTRA:
  33. * 5. If not, please remove something
  34. * 6. Please implement method buy(amount) to your shopping cart.
  35. * This method should return array of Paper Bags with products inside.
  36. * One paper bag can contain 2500g of products.
  37. * 7. How many paper bags do you need?
  38. */
  39.  
  40. // Let's code!
  41. var balance = 35;
  42.  
  43.  
  44. function Product(name, price, type, weight) {
  45. this.name = name;
  46. this.price = price;
  47. this.type = type;
  48. this.weight = weight;
  49. }
  50.  
  51. var soap = new Product("Soap", 3.39, "other", 40);
  52. var cheese = new Product("Cheese", 2.59, "food", 100);
  53. var bread = new Product("Bread", 6.00, "food", 500);
  54. var cola = new Product("Cola", 5.80, "drink", 1500);
  55. var water = new Product("Water", 1.99, "drink", 1000);
  56.  
  57. var shoppingCart = {
  58. items: [],
  59. addToCart: function (item) {
  60. if (item.price <= balance) {
  61. balance -= item.price;
  62. this.items.push(item);
  63. }
  64. },
  65. removeFromCart: function(item) {
  66. if (this.items.indexOf(item) > -1) {
  67. balance += item.price;
  68. this.items.splice(this.items.indexOf(item), 1);
  69. }
  70. }
  71. }
Add Comment
Please, Sign In to add comment