script.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. function htmlTree() {
  2. let body = {
  3. tagName: 'body',
  4. attrs: {},
  5. subTags:[
  6. {
  7. tagName: 'div',
  8. attrs: {},
  9. subTags:[
  10. {
  11. tagName: 'span',
  12. attrs: {},
  13. subTags: 'Enter a data please:'
  14. },
  15. {
  16. tagName: 'br'
  17. },
  18. {
  19. tagName: 'input',
  20. attrs: {
  21. type: 'text',
  22. id: 'name'
  23. },
  24. subTags:[]
  25. },
  26. {
  27. tagName: 'input',
  28. attrs: {
  29. type: 'text',
  30. id: 'surname'
  31. },
  32. subTags:[]
  33. }
  34. ]
  35. },
  36. {
  37. tagName: 'div',
  38. attrs: {},
  39. subTags:[
  40. {
  41. tagName: 'button',
  42. attrs: {
  43. id: 'ok'
  44. },
  45. subTags: 'OK'
  46. },
  47. {
  48. tagName: 'button',
  49. attrs: {
  50. id: 'cancel'
  51. },
  52. subTags: 'Cancel'
  53. }
  54. ]
  55. }
  56. ]
  57. }
  58. console.log(body.subTags[1].subTags[1].subTags);
  59. console.log(body.subTags[0].subTags[3].attrs.id);
  60. } // html tree
  61. function declarativeFields() {
  62. let notebook = {
  63. brand: prompt("Enter a brand"),
  64. type: prompt("Enter a type"),
  65. model: prompt("Enter a model"),
  66. ram: +prompt("Enter a ram"),
  67. size: prompt("Enter a size"),
  68. weight: +prompt("Enter a weight"),
  69. resolution: {
  70. width: +prompt("Enter a width"),
  71. height: +prompt("Enter a height"),
  72. },
  73. };
  74. let phone = {
  75. brand: prompt("Enter a brand"),
  76. model: prompt("Enter a model"),
  77. ram: +prompt("Enter a ram"),
  78. color: prompt("Enter a color")
  79. };
  80. let person = {
  81. name: prompt("Enter a name"),
  82. surname: prompt("Enter a surname"),
  83. married: Boolean(confirm("Married?"))
  84. }
  85. } // declarative fields
  86. function objectLinks() {
  87. let notebook = {
  88. brand: prompt("Enter a brand"),
  89. type: prompt("Enter a type"),
  90. model: prompt("Enter a model"),
  91. ram: +prompt("Enter a ram"),
  92. size: prompt("Enter a size"),
  93. weight: +prompt("Enter a weight"),
  94. resolution: {
  95. width: +prompt("Enter a width"),
  96. height: +prompt("Enter a height"),
  97. },
  98. owner: person
  99. };
  100. let phone = {
  101. brand: prompt("Enter a brand"),
  102. model: prompt("Enter a model"),
  103. ram: +prompt("Enter a ram"),
  104. color: prompt("Enter a color"),
  105. owner: person
  106. };
  107. let person = {
  108. name: prompt("Enter a name"),
  109. surname: prompt("Enter a surname"),
  110. married: Boolean(confirm("Married?")),
  111. smartphone: phone,
  112. laptop: notebook
  113. }
  114. } // object links
  115. function imperativeArrayFill3() {
  116. let arr = [];
  117. arr[arr.length] = prompt('Введите данные');
  118. arr[arr.length] = prompt('Введите данные');
  119. arr[arr.length] = prompt('Введите данные');
  120. alert(arr)
  121. } // imperative array fill 3
  122. function whileConfirm() {
  123. while (true){
  124. if (confirm('Продолжаем?')) break;
  125. }
  126. //while(!confirm());
  127. } // while confirm
  128. function arrayFill() {
  129. let flag = false;
  130. let arr = [];
  131. while (flag === false){
  132. arr.push(prompt('Введите елемент'));
  133. if (arr[arr.length-1] === null){
  134. arr.pop();
  135. flag = true;
  136. }
  137. }
  138. } // array fill
  139. function arrayFillNoPush() {
  140. let flag = false;
  141. let arr = [];
  142. for (let i = 0; flag === false; i++) {
  143. arr[i] = prompt('Введите елемент');
  144. if (arr[arr.length-1] === null) {
  145. arr.pop();
  146. flag = true;
  147. }
  148. }
  149. } // array fill nopush
  150. function infiniteProbability() {
  151. let i = 0;
  152. while (true) {
  153. i++;
  154. if (Math.random() > 0.9){
  155. alert(i);
  156. break;
  157. }
  158. }
  159. } // infinite probability
  160. function emptyLoop() {
  161. while(!prompt('Значение', '0'));
  162. } // empty loop
  163. function progressionSum(N = 20) {
  164. let sum = 0;
  165. for (let i = 1; i < N; i += 3) {
  166. sum += i;
  167. }
  168. alert(sum);
  169. } // progression sum
  170. function chessOneLine(value = 5) {
  171. let str = '';
  172. for (let i = 0; i < value; i++) {
  173. str += ' #';
  174. }
  175. alert(str + ' ');
  176. } // chess one line
  177. function numbers() {
  178. let str = '';
  179. for (let i = 0; i < 10; i++){
  180. for (let j = 0; j < 10; j++){
  181. str += j;
  182. }
  183. str += '\n';
  184. }
  185. alert(str);
  186. } // numbers
  187. function chess(sizeX = 12, sizeY = 10) {
  188. let str = '';
  189. for (let i = 0; i < sizeY; i++) {
  190. for (let j = 0; j < sizeX/2; j++) {
  191. str.length === 0 ? str += '.#' : (str[str.length-2] === '.') ? str += '.#' : str += '#.';
  192. }
  193. str += '\n';
  194. }
  195. alert(str);
  196. } // chess
  197. function cubes(N = 10) {
  198. let arr = [];
  199. for (let i = 0; i < N; i++){
  200. arr.push(Math.pow(i,3));
  201. }
  202. alert(arr);
  203. } // cubes
  204. function multiplyTable() {
  205. let tableMulti = [];
  206. for (let i = 1; i <= 10; i++) {
  207. tableMulti[i] = [];
  208. for (let j = 1; j <= 10; j++) {
  209. tableMulti[i][j-1] = i * j;
  210. }
  211. }
  212. console.log(tableMulti);
  213. } // multiply table
  214. function matrixToHtmlTable() {
  215. let str = '<table>';
  216. let tableMulti = [];
  217. for (let i = 1; i <= 10; i++) {
  218. tableMulti[i] = [];
  219. for (let j = 1; j <= 10; j++) {
  220. tableMulti[i][j-1] = i * j;
  221. }
  222. }
  223. for (let row = 1; row <= tableMulti.length-1; row++) {
  224. str += '<tr>';
  225. for (let col = 0; col < tableMulti.length-1; col++) {
  226. str += `<td>${tableMulti[row][col]}</td>`;
  227. }
  228. str += '</tr>';
  229. }
  230. str += '</table>';
  231. document.querySelector('body').innerHTML = str;
  232. } // matrix to html table
  233. function blueBeltChallengeTheTriangle(sizeX = 11, sizeY = 6) {
  234. let defaultStr = [];
  235. let resultStr = '';
  236. if (sizeX % 2 === 0) sizeX++;
  237. for (let s = 0; s < sizeX; s++){
  238. defaultStr.push('.');
  239. }
  240. for (let i = 0; i < sizeY; i++) {
  241. for (let j = 0; j < 1; j += 3) {
  242. if (i <= 0){
  243. defaultStr[Math.floor(sizeX / 2)] = '#';
  244. resultStr += defaultStr.join('');
  245. }
  246. else {
  247. defaultStr[defaultStr.indexOf('#') - 1] = '#';
  248. defaultStr[defaultStr.lastIndexOf('#') + 1] = '#';
  249. resultStr += defaultStr.join('');
  250. }
  251. }
  252. resultStr += '\n';
  253. }
  254. console.log(resultStr);
  255. } // Задание на синий пояс: Треугольник
  256. //TODO не доделал
  257. function BlackBeltChallengeTheElectronicFortuneTeller () {
  258. let history = [1, 1, 1, 1];
  259. let predictArray = [];
  260. predictArray[0] = [];
  261. predictArray[0][0] = [];
  262. predictArray[0][0][0] = [];
  263. predictArray[0][0][0][0] = [];
  264. let userValue = 0;
  265. for (let i = 0; true; i++){
  266. if (i === 0){
  267. console.log(Math.round(Math.random()));
  268. userValue = +prompt('Введите число от 0 до 1', '0');
  269. if (userValue < 0 || userValue > 1 || isNaN(userValue)){
  270. alert('Вы ввели не верное значение');
  271. continue;
  272. }
  273. predictArray[history[0]][history[1]][history[2]][history[3]] = userValue;
  274. history.push(userValue);
  275. history.shift();
  276. }
  277. else {
  278. console.log(predictArray);
  279. userValue = +prompt('Введите число от 0 до 1', '0');
  280. if (userValue < 0 || userValue > 1 || isNaN(userValue)){
  281. alert('Вы ввели не верное значение');
  282. continue;
  283. }
  284. predictArray[history[0]][history[1]][history[2]][history[3]] = userValue;
  285. history.push(userValue);
  286. history.shift();
  287. }
  288. }
  289. } //Задание на черный пояс: Электронная гадалка