forked from netology-code/js-homeworks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
121 lines (104 loc) · 2.78 KB
/
main.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
"use strict"
console.log("Задача 1")
class BarcodeGenerator {
constructor(size) {
this.size = size;
}
create() {
let id = [];
for (let i = 0; i < this.size; ++i) {
id.push(Math.floor(Math.random() * (10 - 0)) + 0)
}
let prefix = this[BarcodeGenerator.prefix];
if (typeof prefix === 'undefined') {
return `${id.join('')}`
} else {
return `${prefix}-${id.join('')}`
}
}
}
const generator = new BarcodeGenerator(4);
generator[BarcodeGenerator.prefix] = 'AA';
console.log(generator.create());
generator[BarcodeGenerator.prefix] = 'XX';
console.log(generator.create());
console.log(generator.create());
console.log(generator.create());
delete generator[BarcodeGenerator.prefix];
console.log(generator.create());
console.log('Задача 2')
class HexRange {
constructor(from, to) {
this.from = from;
this.to = to;
}
[Symbol.iterator]() {
let a = this.from;
let b = this.to;
return {
next() {
if (a <= b) {
let aHex = a.toString(16);
a++;
return {
done: false,
value: aHex
};
} else {
return {
done: true
};
}
}
};
}
}
let queue = new HexRange(247, 253);
console.log(...queue);
console.log("Задача 3")
class DateRange {
constructor(from, to) {
this.from = from;
this.to = to;
}
[Symbol.iterator]() {
let start = this.from,
end = this.to;
return {
next() {
if (start <= end) {
if (start.getDay() === 0) {
return new Date(start.setDate(start.getDate() + 1));
} else if (start.getDay() === 6) {
return new Date(start.setDate(start.getDate() + 2));
} else {
let dateValue = new Date(start);
if (start.getDay() === 5) {
new Date(start.setDate(start.getDate() + 3));
return {
done: false,
value: dateValue
};
} else {
new Date(start.setDate(start.getDate() + 1));
return {
done: false,
value: dateValue
};
}
}
} else {
return {
done: true
};
}
}
}
}
}
const from = new Date(2017, 2, 13, 23, 59);
const to = new Date(2017, 2, 28, 0, 1);
let range = new DateRange(from, to);
for (let day of range) {
console.log(day.toLocaleDateString('ru-Ru'));
}