-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomework4.js
124 lines (92 loc) · 2.44 KB
/
Homework4.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// JavaScript source code
// 1.
// математическое решение
//function devisionBlender(number) {
// var o = new Object();
// for (var d = 10, j = 1, r = number; r >= d/10; j++ , r -= r % d, d *= 10) {
// o["10^" + j] = (r % d) / (d / 10);
// }
// return o;
//}
// читерское решение, через строки
function devisionBlender(number) {
var o = new Object();
var str = String(number);
for (var i = 0, j = str.length-1; i < str.length; i++, j--) {
o["10^" + j] = str[i];
}
return o;
}
console.log(devisionBlender(9512357));
// 2
//var cart = new Array(
// {
// "quantity": 2,
// "product": {
// "name": "Product1",
// "price": 100
// },
// sum: function () {
// return this.quantity * this.product.price;
// }
// },
// {
// "quantity": 6,
// "product": {
// "name": "Product2",
// "price": 200
// },
// sum: function () {
// return this.quantity * this.product.price;
// }
// },
// {
// "quantity": 5,
// "product": {
// "name": "Product3",
// "price": 300
// },
// sum: function () {
// return this.quantity * this.product.price;
// }
// },
// {
// "quantity": 1,
// "product": {
// "name": "Product4",
// "price": 400
// },
// sum: function () {
// return this.quantity * this.product.price;
// }
// }
//)
//var total_price = cart.reduce((total, item) => total + item.sum(), 0);
//console.log("Total price: " + total_price);
function Product(name, price) {
var o = Object.create(null);
o.name = name;
o.price = price;
return o;
}
function Item(name, price, quantity) {
var item_template = {
sum: function () {
return this.quantity * this.product.price;
}
};
var item = Object.create(item_template);
item.product = Product(name, price);
item.quantity = quantity;
return item;
}
var cart = new Array(
Item("Product1", 1234, 2),
Item("Product2", 2347, 6),
Item("Product3", 3478, 3),
Item("Product4", 41234, 1),
Item("Product5", 5678, 5),
Item("Product6", 6483, 2)
);
var total_price = cart.reduce((total, item) => total + item.sum(item.quantity), 0);
console.log("Total price: " + total_price);