Skip to content

Commit 78e7020

Browse files
committed
update SkillfactoryCoding#10 fathername job generation
1 parent 1451814 commit 78e7020

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

bjs/10_function_object/index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
<div class="row">
2121
<div class="col">
2222
<h3 class="card-title"><span id="surnameOutput">Генерация фамилии</span></h3>
23-
<h4>Имя: <span id="firstNameOutput">Иван</span></h4>
23+
<h4>Имя: <span id="firstNameOutput">Генерация имени</span></h4>
24+
<h4>Отчество: <span id="fatherNameOutput">Генерация отчества</span></h4>
2425
<p>
2526
<span id="genderOutput">Генерация пола</span><!--
2627
--><span>, </span><!--
27-
--><span id="birthYearOutput">Генерация года рождения</span>
28+
--><span id="birthYearOutput">Генерация года рождения</span><!--
29+
--><span>, </span><!--
30+
--><span id="jobOutput">Генерация работы</span>
2831
</p>
2932
</div>
3033
</div>

bjs/10_function_object/init.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ window.onload = function()
44
const initPerson = personGenerator.getPerson();
55
document.querySelector('#firstNameOutput').innerText = initPerson.firstName;
66
document.querySelector('#surnameOutput').innerText = initPerson.surName;
7+
document.querySelector('#fatherNameOutput').innerText = initPerson.fathername;
78
document.querySelector('#genderOutput').innerText = initPerson.gender;
89
document.querySelector('#birthYearOutput').innerText = initPerson.birthday;
10+
document.querySelector('#jobOutput').innerText = initPerson.job;
911
};
1012

bjs/10_function_object/personGenerator.js

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,32 @@ const personGenerator = {
5050
"id_10": "Екатерина"
5151
}
5252
}`,
53+
54+
jobListJson: `{
55+
"count": 20,
56+
"list": {
57+
"id_1": "Повар",
58+
"id_2": "[Ж]Кондитер",
59+
"id_3": "[М]Моряк",
60+
"id_4": "[Ж]Певица",
61+
"id_5": "Штукатур-маляр",
62+
"id_6": "Искусствовед",
63+
"id_7": "Программист",
64+
"id_8": "Переводчик",
65+
"id_9": "[М]Водитель",
66+
"id_10": "[М]Спортивный тренер",
67+
"id_11": "Психолог",
68+
"id_12": "Адвокат",
69+
"id_13": "[М]Сварщик",
70+
"id_14": "Маркетолог",
71+
"id_15": "Врач",
72+
"id_16": "Бухгалтер",
73+
"id_17": "[М]Шахтер",
74+
"id_18": "[Ж]Актриса",
75+
"id_19": "[М]Электрик",
76+
"id_20": "[М]Военнослужащий"
77+
}
78+
}`,
5379

5480
GENDER_MALE: 'Мужчина',
5581
GENDER_FEMALE: 'Женщина',
@@ -62,9 +88,39 @@ const personGenerator = {
6288
return obj.list[prop];
6389
},
6490

91+
randomValueJob: function () {
92+
//Список берется напрямую из объекта, а не через параметр функции
93+
const obj = JSON.parse(this.jobListJson);
94+
95+
// В названии профессий может быть марка гендерной привязанности
96+
// Нам нужно определить, сколько профессий в списке можно причислить к текущему полу
97+
let genderMark = this.person.gender === this.GENDER_MALE? "[Ж]" : "[М]";
98+
99+
let newCount = obj.count;
100+
for (let j = 1; j <= obj.count; j++)
101+
if ( String(obj.list[`id_${j}`]).includes(genderMark)) newCount--;
102+
103+
// Потом по полученному количеству генерируем случайное число
104+
// и сразу в цикле for определяем, можем ли мы привязать случайно выбранную профессию
105+
// Если нет, то переходим к следущему варианту в списке.
106+
// ========================================================
107+
// Цикл for позволяет сразу установить значение счетчику, проверить значение по счетчику и изменить счетчик
108+
// три зайца одним выстрелом (в одну строку), хоть и можно считать ниндзя-кодом
109+
// Такая конструкция нам позволяет из одного списка профессий выбрать только необходимые, если критерий содержится в значении
110+
let i;
111+
for (i = this.randomIntNumber(newCount, 1); String(obj.list[`id_${i}`]).includes(genderMark) ; i++) ;
112+
113+
// Удаляем марку из названия
114+
genderMark = this.person.gender === this.GENDER_MALE? "[М]" : "[Ж]";
115+
116+
// И выводим.
117+
return String(obj.list[`id_${i}`]).replace(genderMark, "");
118+
},
119+
65120
randomFirstName: function() {
66121

67122
if (this.person.gender === this.GENDER_MALE)
123+
68124
{
69125
return this.randomValue(this.firstNameMaleJson);
70126
}
@@ -75,7 +131,6 @@ const personGenerator = {
75131

76132
},
77133

78-
79134
randomSurname: function() {
80135

81136
let result = this.randomValue(this.surnameJson);
@@ -89,6 +144,36 @@ const personGenerator = {
89144

90145
},
91146

147+
randomFathername: function() {
148+
149+
let result = String(this.randomValue(this.firstNameMaleJson));
150+
151+
switch (result[result.length - 1]) {
152+
case "й":
153+
result = result.substring(0, result.length - 1) + "ев"
154+
break;
155+
156+
case "а":
157+
result = result.substring(0, result.length - 1);
158+
159+
default:
160+
result = result + "ов";
161+
break;
162+
}
163+
164+
if (this.person.gender === this.GENDER_MALE)
165+
{
166+
result = result + "ич";
167+
}
168+
else
169+
{
170+
result = result + "на";
171+
}
172+
173+
return result;
174+
175+
},
176+
92177
randomGender: function() {
93178

94179
return this.randomIntNumber() === 1 ? this.GENDER_MALE : this.GENDER_FEMALE;
@@ -129,7 +214,9 @@ const personGenerator = {
129214
this.person.gender = this.randomGender();
130215
this.person.firstName = this.randomFirstName();
131216
this.person.surName = this.randomSurname();
217+
this.person.fathername = this.randomFathername()
132218
this.person.birthday = this.randomDate();
219+
this.person.job = this.randomValueJob();
133220
return this.person;
134221
}
135222
};

0 commit comments

Comments
 (0)