-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
176 lines (143 loc) · 5.21 KB
/
map.js
File metadata and controls
176 lines (143 loc) · 5.21 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//TODO checkPassword сделает код более чистым и переиспользуемым - реализуйте все проверки пароля через него
//Добавить внесение тоже с проверкой пароля
function createUser(login, pass, startBalance = 0) {
let balance = startBalance;
return {
getLogin: () => login,
checkPassword: (p) => pass === p,
getBalance(p) {
if (!this.checkPassword(p)) {
console.log("Wrong password!");
return null;
}
console.log(`Your balance: ${balance}`);
return balance;
},
deposit(amount, p) {
if (!this.checkPassword(p)) {
console.log("Wrong password!");
return null;
}
balance += amount;
console.log(`Deposited ${amount}. New balance: ${balance}`);
return balance;
}
};
}
const user = createUser("user", "1234", 1000);
//TODO 2 Дан массив чисел. Получить новый массив, где каждое число возведено в квадрат.
const nums = [1, 2, 3, 4];
const squared = nums.map(n => n ** 2);
console.log(squared);
//TODO 3 Дан массив строк const users = ["Anna", "Ivan", "Olga"];. Построить новый массив строк вида:["1/3: Anna", "2/3: Ivan", "3/3: Olga"] (добавляется номер по порядку и длина)
const users = ["Anna", "Ivan", "Olga"];
const formatted = users.map((name, index, arr) => {
return `${index + 1}/${arr.length}: ${name}`;
});
console.log(formatted);
//TODO 4 Есть массив пользователей с годом рождения.
// Нужно для каждого посчитать возраст и добавить флаг isAdult.
const rawUsers = [
{ name: "Anna", birthYear: 2000 },
{ name: "Ivan", birthYear: 2010 }
];
const currentYear = 2025;
const usersWithAge = rawUsers.map(user => {
const age = currentYear - user.birthYear;
return {
...user,
age,
isAdult: age >= 18
};
});
console.log(usersWithAge);
// Ожидаемый результат
// [
// { name: "Anna", birthYear: 2000, age: 25, isAdult: true },
// { name: "Ivan", birthYear: 2010, age: 15, isAdult: false }
// ]
//TODO 5 Есть заказы с техническими статусами. Нужно добавить человеко-понятный текст статуса.
const orders1 = [
{ id: 1, status: "pending" },
{ id: 2, status: "processing" },
{ id: 3, status: "done" },
{ id: 4, status: "rejected" }
];
const statusMap = {
pending: "Ожидает подтверждения",
processing: "В обработке",
done: "Выполнено",
rejected: "Отклонено"
};
const withStatusText = orders1.map(order => ({
...order,
statusText: statusMap[order.status]
}));
console.log(withStatusText);
// ожидаемый результат
// [
// { id: 1, status: "pending", statusText: "Ожидает подтверждения" },
// ...
// ]
//TODO 6 (со звездочкой)
// Есть исходный массив
const orders2 = [
{
id: 1,
customerName: "Anna",
status: "paid", // new | paid | shipped | cancelled
total: 1200, // сумма в рублях
createdAt: "2025-11-01T10:15:00Z"
},
{
id: 2,
customerName: "Ivan",
status: "new",
total: 300,
createdAt: "2025-11-18T09:00:00Z"
},
{
id: 3,
customerName: "Olga",
status: "shipped",
total: 7000,
createdAt: "2025-10-20T14:30:00Z"
},
{
id: 4,
customerName: "Petr",
status: "cancelled",
total: 1500,
createdAt: "2025-11-10T12:00:00Z"
}
];
const humanStatus = {
new: "Новый заказ",
paid: "Оплачен",
shipped: "Отправлен",
cancelled: "Отменён"
};
const result = orders2
.filter(o => (o.status === "paid" || o.status === "shipped") && o.total >= 1000)
.map(o => {
const date = new Date(o.createdAt)
.toLocaleDateString("ru-RU"); // 01.11.2025
return {
title: `Заказ #${o.id} от ${date}`,
subtitle: `Клиент: ${o.customerName}`,
statusText: humanStatus[o.status],
totalFormatted: `${o.total.toLocaleString("ru-RU")} NIS`,
isBigOrder: o.total >= 5000
};
});
console.log(result);
//Задача
//
// Оставить только те заказы, которые: оплачены или отправлены (status === "paid" или "shipped"),
//и при этом сумма заказа не меньше 1000.
//Преобразовать каждый такой заказ в объект “для интерфейса”:
//title: строка вида "Заказ #1 от 01.11.2025";
// subtitle: "Клиент: Anna";
// statusText: человеко-понятный статус;
// totalFormatted: "1 200 NIS";
//isBigOrder: true / false — крупный заказ (сумма ≥ 5000).