123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- 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 = "<table border='1'>"
- for (let {...obj} of finalArr) {
- str += '<tr>';
- for (const objKey in obj) {
- str += `<td>${objKey}</td><td>${obj[objKey]}</td>`;
- }
- str += '</tr>';
- }
- str += "</table>"
- console.log(str);
- document.write(str);
- } // HTML
- function htmlOptionalFields() {
- let finalArr = deserialize();
- let arrTh = [];
- let str = "<table border='1'>"
- str += '<tr>';
- for (let {...obj} of finalArr) {
- for (const objKey in obj) {
- if (str.includes(objKey)) continue;
- str += `<th>${objKey}</th>`;
- arrTh.push(objKey);
- }
- }
- str += '</tr>';
- let arrMiddle = [];
- for (let {...obj} of finalArr) {
- for (const objKey in obj) {
- arrMiddle[arrTh.indexOf(objKey)] = obj[objKey];
- }
- str += `<tr>`;
- for (let i = 0; i < arrTh.length; i++) {
- if (arrMiddle[i] === undefined){
- str += `<td></td>`
- }
- else{
- str += `<td>${arrMiddle[i]}</td>`
- }
- }
- str += `</tr>`;
- }
- str += "</table>"
- console.log(str);
- document.write(str);
- } // HTML optional fields
- function htmlTrColor() {
- let finalArr = deserialize();
- let arrTh = [];
- let str = "<table border='1'>"
- str += '<tr>';
- for (let {...obj} of finalArr) {
- for (const objKey in obj) {
- if (str.includes(objKey)) continue;
- str += `<th>${objKey}</th>`;
- arrTh.push(objKey);
- }
- }
- str += '</tr>';
- 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 += `<tr style="background-color: #868585">`;
- }
- else
- {
- str += `<tr>`;
- }
- for (let i = 0; i < arrTh.length; i++) {
- if (arrMiddle[i] === undefined){
- str += `<td></td>`
- }
- else{
- str += `<td>${arrMiddle[i]}</td>`
- }
- }
- str += `</tr>`;
- }
- str += "</table>"
- console.log(str);
- document.write(str);
- } // HTML tr color
- function htmlThOptional() {
- let finalArr = deserialize();
- let arrTh = [];
- let str = "<table border='1'>"
- str += '<tr>';
- for (let {...obj} of finalArr) {
- for (const objKey in obj) {
- if (str.includes(objKey)) continue;
- str += `<th>${objKey}</th>`;
- arrTh.push(objKey);
- }
- }
- str += '</tr>';
- 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 += `<tr style="background-color: #868585">`;
- }
- else
- {
- str += `<tr>`;
- }
- for (let i = 0; i < arrTh.length; i++) {
- if (arrMiddle[i] === undefined){
- str += `<td></td>`
- }
- else{
- str += `<td>${arrMiddle[i]}</td>`
- }
- }
- str += `</tr>`;
- }
- str += "</table>"
- 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 не доделал
|