let a = {
name: 'Vlad',
surname: 'Marchenko',
} //3 persons
let b = {
name: 'Vitalii',
surname: 'Marchenko',
} //3 persons
let c = {
name: 'Dmitruy',
surname: 'Mitrohin',
} //3 persons
function differentFields() {
console.log('different fields');
a.age = 30;
b.fathername = 'Ivanov';
c.sex = 'male';
} // different fields
function fieldsCheck() {
differentFields();
console.log('fields check');
if ('age' in a) {
console.log(a.age);
}
if (typeof b['fathername'] !== "undefined") {
console.log(b.fathername);
}
} // fields check
function arrayOfPersons() {
fieldsCheck();
console.log('array of persons');
let person = [{...a}, {...b}, {...c}, {name: 'Alex', surname: 'Reznichenko', fathername: 'God'}];
console.log(person);
return person;
} // array of persons
function loopOfPersons() {
let person = arrayOfPersons();
console.log('loop of persons');
for (let item of person) {
console.log(item);
}
} // loop of persons
function loopOfNameAndSurname() {
let person = arrayOfPersons();
console.log('loop of name and surname');
for (let {...obj} of person) {
if('name' in obj && 'surname' in obj) {
console.log(obj['name'], obj['surname']);
}
}
} // loop of name and surname
function loopOfLoopOfValues() {
let person = arrayOfPersons();
console.log('loop of loop of values');
for (let i = 0; i < person.length; i++) {
for (let key in person[i]) {
console.log(person[i][key]);
}
}
} // loop of loop of values
function fullName() {
let person = arrayOfPersons();
console.log('fullName');
person.forEach(item => {
if('name' in item && 'surname' in item && 'fathername' in item){
item.fullName = `${item['name']} ${item['surname']} ${item['fathername']}`;
}
});
console.log(person);
return person;
} // fullName
function serialize() {
let person = fullName();
console.log('serialize');
let str = JSON.stringify(person, null, 4)
console.log(str);
return str;
} // serialize
function deserialize() {
let str = serialize();
let person = fullName();
console.log('deserialize');
let betweenArr = JSON.parse(str);
for (let betweenArrElement of betweenArr) {
person.push(betweenArrElement);
break;
}
console.log('Final array', person);
return person;
} // deserialize
// loopOfPersons(); // вывод всего массива persons
// loopOfNameAndSurname(); // вывод имени и фамилии массива persons
// loopOfLoopOfValues(); // вывод значений из массива объектов
// все функции связаны между собой (получают значения по цепочке), вызывать в порядке расположения
//******************HTML*******************//
function html() {
let finalArr = deserialize();
let str = "
"
for (let {...obj} of finalArr) {
str += '';
for (const objKey in obj) {
str += `${objKey} | ${obj[objKey]} | `;
}
str += '
';
}
str += "
"
console.log(str);
document.write(str);
} // HTML
function htmlOptionalFields() {
let finalArr = deserialize();
let arrTh = [];
let str = ""
str += '';
for (let {...obj} of finalArr) {
for (const objKey in obj) {
if (str.includes(objKey)) continue;
str += `${objKey} | `;
arrTh.push(objKey);
}
}
str += '
';
let arrMiddle = [];
for (let {...obj} of finalArr) {
for (const objKey in obj) {
arrMiddle[arrTh.indexOf(objKey)] = obj[objKey];
}
str += ``;
for (let i = 0; i < arrTh.length; i++) {
if (arrMiddle[i] === undefined){
str += ` | `
}
else{
str += `${arrMiddle[i]} | `
}
}
str += `
`;
}
str += "
"
console.log(str);
document.write(str);
} // HTML optional fields
function htmlTrColor() {
let finalArr = deserialize();
let arrTh = [];
let str = ""
str += '';
for (let {...obj} of finalArr) {
for (const objKey in obj) {
if (str.includes(objKey)) continue;
str += `${objKey} | `;
arrTh.push(objKey);
}
}
str += '
';
let counter = 0;
let arrMiddle = [];
for (let {...obj} of finalArr) {
for (const objKey in obj) {
arrMiddle[arrTh.indexOf(objKey)] = obj[objKey];
}
counter++;
if (counter % 2 === 0){
str += ``;
}
else
{
str += `
`;
}
for (let i = 0; i < arrTh.length; i++) {
if (arrMiddle[i] === undefined){
str += ` | `
}
else{
str += `${arrMiddle[i]} | `
}
}
str += `
`;
}
str += "
"
console.log(str);
document.write(str);
} // HTML tr color
function htmlThOptional() {
let finalArr = deserialize();
let arrTh = [];
let str = ""
str += '';
for (let {...obj} of finalArr) {
for (const objKey in obj) {
if (str.includes(objKey)) continue;
str += `${objKey} | `;
arrTh.push(objKey);
}
}
str += '
';
let counter = 0;
let arrMiddle = [];
for (let {...obj} of finalArr) {
for (const objKey in obj) {
arrMiddle[arrTh.indexOf(objKey)] = obj[objKey];
}
counter++;
if (counter % 2 === 0){
str += ``;
}
else
{
str += `
`;
}
for (let i = 0; i < arrTh.length; i++) {
if (arrMiddle[i] === undefined){
str += ` | `
}
else{
str += `${arrMiddle[i]} | `
}
}
str += `
`;
}
str += "
"
console.log(str);
document.write(str);
} // HTML th optional
//html(); // простой вывод массива в html
//htmlOptionalFields(); // вывод масива по колонкам
//htmlTrColor(); // вывод масива по колонкам + цвет
//htmlThOptional(); // вывод масива по колонкам
function blueBelt(someTree, str = '') {
if ('tagName' in someTree) {
if ('attrs' in someTree) {
let artStr = '';
for (const atr in someTree['attrs']) {
artStr += `${atr}: ${someTree['attrs'][atr]};`;
}
str += `<${someTree['tagName']} style=\"${artStr}\">`;
}
else{
str += `<${someTree['tagName']}>`;
}
}
if ('text' in someTree) {
str += `${someTree['text']}`;
}
if ('subTags' in someTree) {
for (let i = 0; i < someTree['subTags'].length; i++) {
str = blueBelt(someTree['subTags'][i], str);
}
}
str += `${someTree['tagName']}>`;
return str;
} // задание на синий пояс
let someTree = {
tagName: "table", //html tag
subTags: [ //вложенные тэги
{
tagName: "tr",
subTags: [
{
tagName: "td",
text: "some text",
},
{
tagName: "td",
text: "some text 2",
}
]
}
],
attrs:
{
border: 1,
margin: 10,
padding: 1,
},
} // тестовые данные для задания на синий пояс
//console.log(blueBelt(someTree)); // вызов задания на синий пояс
function destructArray() {
let arr = [1,2,3,4,5, "a", "b", "c"];
let [odd1, even1, odd2, even2, odd3, ...str] = arr;
console.log(odd1, even1, odd2, even2, odd3, str);
} // destruct array
function destructString() {
let arr = [1, "abc"];
let [number, [s1, s2, s3]] = arr;
console.log(number, s1, s2, s3);
} // destruct string
function destruct2() {
let obj = {
name: 'Ivan',
surname: 'Petrov',
children: [
{name: 'Maria'},
{name: 'Nikolay'}
]
}
let [{name: name1}, {name: name2}] = obj.children;
console.log(name1, name2);
} // destruct 2
function destruct3() {
let arr = [1,2,3,4, 5,6,7,10];
let {0:a, 1:b, length} = arr;
console.log(a, b, length);
} // destruct 3
//destructArray();
//destructString();
//destruct2();
//destruct3();
function blackBelt() {
let history = [1, 1, 1, 1];
let predictArray = [];
predictArray[0] = [];
predictArray[0][0] = [];
predictArray[0][0][0] = [];
predictArray[0][0][0][0] = [];
let userValue = 0;
for (let i = 0; true; i++){
if (i === 0){
console.log(Math.round(Math.random()));
userValue = +prompt('Введите число от 0 до 1', '0');
if (userValue < 0 || userValue > 1 || isNaN(userValue)){
alert('Вы ввели не верное значение');
continue;
}
predictArray[history[0]][history[1]][history[2]][history[3]] = userValue;
history.push(userValue);
history.shift();
}
else {
console.log(predictArray);
userValue = +prompt('Введите число от 0 до 1', '0');
if (userValue < 0 || userValue > 1 || isNaN(userValue)){
alert('Вы ввели не верное значение');
continue;
}
predictArray[history[0]][history[1]][history[2]][history[3]] = userValue;
history.push(userValue);
history.shift();
}
}
} // задание на черный пояс TODO не доделал