-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise-7.js
More file actions
90 lines (72 loc) · 2.61 KB
/
exercise-7.js
File metadata and controls
90 lines (72 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
E-commerce
You have to create an e-commerce shopping cart
First of all we need 2 arrays,
the list of available products and the list of products introduced in the shopping cart.
1. Fill the products list with at least 4 products, what attributes de we have?
at least should have:
var product = {
id:
name:
price:
stock:
}
id is a unique number that identifies each product
3. Create a function addToShoppingCart to add a product to the shopping cart list giving the product id,
the function will add the product to the selectedProduct list, and add the price to the totalPrice
4. Create the function removeFrom ShoppingCart to remove a product that a client does not like anymore
5. Create the function shop, the function will empty the list and set 0 in the totalPrice of the shopping cart
In addition will substract 1 in the product stock of bought products
6. If there is not enough stock, the product cannot be added to the shopping cart
*/
var products = [];
var product1 = {
id: 1,
name: "Toaster X56 Plus",
price: 12.98,
stock: 105
};
var product2 = {
id: 2,
name: "Watch Rocker",
price: 9.99,
stock: 2
};
products.push(product1);
products.push(product2);
var shoppingCart = {
totalPrice: 0,
selectedProducts: []
};
function addToShoppingCart(id){
}
function removeFromShoppingCart(id){
}
function shop(){
}
//results
addToShoppingCart(1);
console.log("Step 1");
console.log("Total Price = " + shoppingCart.totalPrice);
console.log("Number of Elements = " + shoppingCart.selectedProducts.length);
console.log("Name of Elements = " + shoppingCart.selectedProducts.map(p=>p.name));
addToShoppingCart(2);
console.log("Step 2");
console.log("Total Price = " + shoppingCart.totalPrice);
console.log("Number of Elements = " + shoppingCart.selectedProducts.length);
console.log("Name of Elements = " + shoppingCart.selectedProducts.map(p=>p.name));
addToShoppingCart(4);
console.log("Step 3");
console.log("Total Price = " + shoppingCart.totalPrice);
console.log("Number of Elements = " + shoppingCart.selectedProducts.length);
console.log("Name of Elements = " + shoppingCart.selectedProducts.map(p=>p.name));
removeFromShoppingCart(2);
console.log("Step 4");
console.log("Total Price = " + shoppingCart.totalPrice);
console.log("Number of Elements = " + shoppingCart.selectedProducts.length);
console.log("Name of Elements = " + shoppingCart.selectedProducts.map(p=>p.name));
shop();
console.log("Step 5");
console.log("Total Price = " + shoppingCart.totalPrice);
console.log("Number of Elements = " + shoppingCart.selectedProducts.length);
console.log("Name of Elements = " + shoppingCart.selectedProducts.map(p=>p.name));