Browse Source

HW <04> "ДЗ: Вложенные декларативные структуры и код в них. Отображение циклических и древовидных структур. Циклы." done

Vladimir 2 years ago
parent
commit
00ac3b83bf
2 changed files with 317 additions and 0 deletions
  1. 12 0
      HW 04/index.html
  2. 305 0
      HW 04/main.js

+ 12 - 0
HW 04/index.html

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <script src="main.js"></script>
+</body>
+</html>

+ 305 - 0
HW 04/main.js

@@ -0,0 +1,305 @@
+//html tree
+let loginForm = {
+    tagName: "body",
+    subTags: [
+        {
+            tagName: "div",
+            subTags: [
+                {
+                    tagName: "span",
+                    text: "Enter a data please:"
+                },
+                {
+                    tagName: "input",
+                    attrs: {
+                        type: "text",
+                        id: "name"
+                    }
+                },
+                {
+                    tagName: "input",
+                    attrs: {
+                        type: "text",
+                        id: "surname"
+                    }  
+                }
+            ]
+        },
+        {
+            tagName: "div",
+            subTags: [
+                {
+                    tagName: "button",
+                    attrs: {
+                        id: "ok"
+                    },
+                    text: "OK"
+                },
+                {
+                    tagName: "button",
+                    attrs: {
+                        id: "cancel"
+                    },
+                    text: "Cancel"
+                }
+            ]
+        }
+    ]
+}
+
+console.log(loginForm.subTags[1].subTags[1].text);
+console.log(loginForm.subTags[0].subTags[2].attrs.id);
+
+
+//declarative fields
+var notebook = {
+    brand: prompt("Бренд ноутбука"),
+    type:  prompt("Тип ноутбука"),
+    model: prompt("Модель ноутбука"),
+    ram: +prompt("Ядра ноутбука"),
+    size: prompt("Размер"),
+    weight: +prompt("Вес"),
+    resolution: {
+        width: +prompt("Ширина экрана"),
+        height: +prompt("Высота экрана"),
+    },
+};
+
+var phone = {
+    brand: prompt("Бренд телефона"),
+    model: prompt("Модель телефона"),
+    ram: +prompt("Ядер"),
+    color: prompt("Цвет телефона"),
+};
+
+var person = {
+    name: prompt("Имя"),
+    surname: prompt("Фамилия"),
+    married: confirm("Есть жена\муж ?"),
+}
+
+
+//object links
+var notebook1 = {
+    brand: "HP",
+    type:  "440 G4",
+    model: "Y7Z75EA",
+    ram: 4,
+    size: "14",
+    weight: 1.8,
+    resolution: {
+        width: 1920,
+        height: 1080,
+    },
+};
+
+var phone1 = {
+    brand: "meizu",
+    model: "m2",
+    ram: 2,
+    color: "black",
+};
+
+var person1 = {
+    name: "Donald",
+    surname: "Trump",
+    married: true,
+}
+
+person1.smartphone = phone1;
+person1.laptop = notebook1;
+notebook1.owner = person1;
+phone1.owner = person1;
+
+console.log(person1.smartphone.owner.laptop.owner.smartphone == person1.smartphone);
+
+
+//imperative array fill 3
+let newArr = [];
+
+for(let i = 0; i < 3; i++) {
+    newArr.push(prompt(`Введите элемент номер ${i + 1}`));
+}
+
+console.log(newArr);
+
+
+//while confirm
+let check;
+
+do{
+    check = confirm("Для продолжения нажмите 'Отмена'");
+}while(!check);
+
+
+//array fill
+let check1;
+let newArr1 = [];
+
+while(!check1) {
+    newArr1.push("Какой-то элемент");
+    check1 = confirm("Для добавления нового элемента в массив нажмите 'Отмена'");
+}
+
+console.log(newArr1);
+
+
+//array fill nopush
+let check2;
+let newArr2 = [];
+let count = 0;
+
+while(!check2) {
+    newArr2[count] = ("Какой-то элемент");
+    check2 = confirm("Для добавления нового элемента в массив нажмите 'Отмена'");
+    count++;
+}
+
+console.log(newArr2);
+
+
+//infinite probability
+let count1 = 0;
+
+while(true){
+    if(Math.random() > 0.9){
+        break;
+    }
+    console.log(count1++);
+}
+
+
+//empty loop
+let check3;
+
+while(!check3) check3 = prompt("Нажмите 'Отмена' что бы продолжить");
+
+
+//chess one line
+let str = " ";
+
+for(let i = 0; i < 5; i++) {
+    str += "# ";
+}
+
+console.log(str);
+
+
+//numbers
+let str1 = "";
+
+for(let i = 0; i < 10; i++) {
+    str1 += "0123456789\n";
+}
+
+console.log(str1);
+
+
+//chess
+let str2 = "";
+
+for(let i = 0; i < 10; i++) {
+    if(i % 2) {
+        str2 += "#";
+    } else {
+        str2 += ".";
+    }
+    for(let j = 0; j < 10; j++) {
+        if((j + i) % 2) {
+            str2 += ".";
+        } else {
+            str2 += "#";
+        }
+    }
+    str2 += "\n";
+}
+
+console.log(str2);
+
+
+//cubes
+let newArr3 = [];
+
+let n1 = 7;
+
+for(let i = 0; i < n1; i++) {
+    newArr3[i] = Math.pow(i, 3);
+}
+
+console.log(newArr3);
+
+
+//multiply table
+let multiplicationTableArr = [];
+
+for(let i = 0; i < 10; i++) {
+    multiplicationTableArr[i] = [];
+    for(let j = 0; j < 10; j++) {
+        multiplicationTableArr[i][j] = i * j;
+    }
+}
+
+console.log(multiplicationTableArr);
+
+
+//matrix to html table
+let str3 = "";
+
+str3 += "<table>";
+
+for(let i = 0; i < multiplicationTableArr.length; i++) {
+    str3 += "<tr>";
+    for(let j = 0; j < multiplicationTableArr[i].length; j++) {
+        str3 += `<td>${multiplicationTableArr[i][j]}</td>`;
+    }
+    str3 += "</tr>";
+}
+
+str3 += "</table>";
+
+document.write(str3);
+
+
+//Задание на синий пояс: Треугольник
+let str4 = "";
+
+for(let i = 0; i < 6; i++) {
+    for(let j = 0; j < 5 - i; j++) {
+        str4 += ".";
+    }
+    for(let j = 0; j < i; j++) {
+        if(j > 0) {
+            str4 += "##";
+        } else {
+            str4 += "#";
+        }
+    }
+    for(let j = 0; j < 5- i; j++) {
+        str4 += ".";
+    }
+    str4 += "\n";
+}
+
+console.log(str4);
+
+
+//Задание на черный пояс: Электронная гадалка
+let predictArray = [
+    [1, 1],
+    [1, 1]
+]
+
+let history = [1, 1];
+
+let userChose = 1;
+
+while(userChose) {
+    userChose = prompt("Введите 0 или 1");
+
+    alert(`Предсказываю ${predictArray[history[0]][history[1]]}`);
+
+    predictArray[history[0]][history[1]] = +userChose;
+
+    history.push(+userChose);
+    history.shift();
+}