-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoppingcart.js
90 lines (75 loc) · 1.63 KB
/
shoppingcart.js
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
let cart = [
{
name : "banana",
qty : 3,
price : 4.99
},
{
name : "chicken",
qty : 2,
price : 3.99
},
{
name : "apple",
qty : 5,
price : 2.99
},
{
name : "milk",
qty : 1,
price : 1.70
},
{
name : "pork",
qty : 7,
price : 3.79
},
{
name : "orange",
qty : 2,
price : 3.00
},
{
name : "toothpaste",
qty : 1,
price : 1.50
},
]
function counter( obj ) {
x = []
for (var num in obj) {
x.push(obj[num].name)
}
return x
}
randum = counter(cart)
function sum_res ( obj ) {
sum = 0;
sum_1 = []
for (let items in obj) {
result = obj[items].price * obj[items].qty
sum += result
}
return sum
}
function mult_res( obj ) {
sum_1 = []
for (let items in obj) {
result = obj[items].price * obj[items].qty
sum_1.push(result)
}
return sum_1
}
result1 = sum_res(cart)
result2 = mult_res(cart)
function drawer( res1, res2, cart ) {
if (cart.length == 0) {
console.log("There arent any items on the cart")
}
for (let item in cart) {
console.log("Item " + item + ": " + cart[item].name + " " + cart[item].price + "€ " + cart[item].qty)
}
console.log("---------------------")
console.log("Total: " + cart.length + " " + "Items: " + randum + " Total price: " + result1)
}
console.log(drawer(result1, result2, cart))