index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // htmlTree()
  2. function htmlTree() {
  3. let body = {
  4. tagName: 'body',
  5. attrs: {},
  6. children: [
  7. { tagName: 'div',
  8. attrs: {},
  9. children: [
  10. { tagName: 'span',
  11. attrs: {},
  12. children: 'Enter a data please'
  13. },
  14. { tagName: 'br',
  15. attrs: {}
  16. },
  17. { tagName: 'input',
  18. attrs: {
  19. type: 'text',
  20. id: 'name'
  21. }
  22. },
  23. { tagName: 'input',
  24. attrs: {
  25. type: 'text',
  26. id: 'surname'
  27. }
  28. }
  29. ]
  30. },
  31. { tagName: 'div',
  32. attrs: {},
  33. children: [
  34. { tagName: 'button',
  35. attrs: {
  36. id: 'ok'
  37. },
  38. children: 'OK'
  39. },
  40. { tagName: 'button',
  41. attrs: {
  42. id: 'cansel'
  43. },
  44. children: 'Cancel'
  45. }
  46. ]
  47. }
  48. ]
  49. }
  50. console.log(body.children[1].children[1].children)
  51. console.log(body.children[0].children[3].attrs.id)
  52. }
  53. // declarativeFields()
  54. function declarativeFields() {
  55. var notebook = {
  56. brand: prompt("Enter a brand",""),
  57. type: prompt("Enter a type",""),
  58. model: prompt("Enter a model",""),
  59. ram: +prompt("Enter amount of ram (gb)",""),
  60. size: prompt("Enter a size of screen (inch)",""),
  61. weight: +prompt("Enter a weight (kg)",""),
  62. resolution: {
  63. width: +prompt("Enter a resolution width (px)",""),
  64. height: +prompt("Enter a resolution height (px)",""),
  65. },
  66. };
  67. console.log(notebook);
  68. var phone = {
  69. brand: prompt("Enter a brand",""),
  70. model: prompt("Enter a model",""),
  71. ram: +prompt("Enter amount of ram (gb)",""),
  72. color: prompt("Enter a color",""),
  73. };
  74. console.log(phone);
  75. var person = {
  76. name: prompt("Enter a name",""),
  77. surname: prompt("Enter a surname",""),
  78. married: confirm("Is person married?",""),
  79. }
  80. console.log(person);
  81. }
  82. // tableMult()
  83. function tableMult() {
  84. let str = '<table>'
  85. for (let rowIndex=1; rowIndex<10; rowIndex++) {
  86. str += '<tr>'
  87. for (let j=1; j<10; j++) {
  88. str += `<td>${rowIndex*j}</td>`
  89. }
  90. str += '</tr>'
  91. }
  92. str += '</table>'
  93. document.write(str)
  94. }
  95. // contArr()
  96. function contArr() {
  97. let inputArr = [1,2,3,4,5,6,'qqq',true]
  98. let newElement = 0
  99. for (let i=0; i<inputArr.length; i++) {
  100. newElement += inputArr[i]
  101. }
  102. inputArr.push(newElement)
  103. console.log(inputArr)
  104. }
  105. // tableMultObj()
  106. function tableMultObj() {
  107. let phone = {
  108. brand: "meizu",
  109. model: "m2",
  110. ram: 2,
  111. color: "black",
  112. uuuuu: true
  113. }
  114. let str = '<table>\n'
  115. for (const [key, value] of Object.entries(phone)) {
  116. str += ` \n<tr><td><b>${key}</b></td><td>${value}</td></tr>\n`
  117. }
  118. str += '</table>'
  119. document.write(str)
  120. }
  121. // objectLinks()
  122. function objectLinks() {
  123. var notebook = {
  124. brand: "HP",
  125. type: "440 G4",
  126. model: "Y7Z75EA",
  127. ram: 4,
  128. size: "14",
  129. weight: 1.8,
  130. resolution: {
  131. width: 1920,
  132. height: 1080,
  133. },
  134. };
  135. var phone = {
  136. brand: "meizu",
  137. model: "m2",
  138. ram: 2,
  139. color: "black",
  140. };
  141. var person = {
  142. name: "Donald",
  143. surname: "Trump",
  144. married: true,
  145. };
  146. person.smartphone = phone;
  147. person.laptop = notebook;
  148. phone.owner = person;
  149. notebook.owner = person;
  150. console.log(person.smartphone.owner.laptop.owner.smartphone == person.smartphone);
  151. }
  152. // imperativeArrayFill3()
  153. function imperativeArrayFill3() {
  154. let arr = [];
  155. arr[0] = prompt("Вводи элемент массива","")
  156. arr[1] = prompt("Вводи элемент массива","")
  157. arr[2] = prompt("Вводи элемент массива","")
  158. console.log(arr);
  159. }
  160. // whileConfirm()
  161. function whileConfirm() {
  162. let a = true;
  163. while (a) {
  164. a = confirm('Продолжить цикл?')
  165. }
  166. }
  167. // arrayFill()
  168. function arrayFill() {
  169. let arr = [];
  170. let res = null;
  171. do {
  172. res = prompt("Вводи элемент массива","");
  173. if (res !== null) {
  174. arr.push(res);
  175. }
  176. } while (res !== null)
  177. console.log(arr);
  178. }
  179. // arrayFillNopush()
  180. function arrayFillNopush() {
  181. let arr = [];
  182. let res = null;
  183. let i = 0;
  184. do {
  185. res = prompt("Вводи элемент массива","");
  186. if (res !== null) {
  187. arr[i] = res;
  188. }
  189. i++;
  190. } while (res !== null)
  191. console.log(arr);
  192. }
  193. // infiniteProbability()
  194. function infiniteProbability() {
  195. let i = 0;
  196. while (true) {
  197. if (Math.random() > 0.9) {
  198. break;
  199. }
  200. i++;
  201. }
  202. console.log(`Количество итераций: ${i}`);
  203. }
  204. // emptyLoop()
  205. function emptyLoop() {
  206. for (let i = 0; i !== null; i = prompt("Продолжить цикл?","")) {}
  207. }
  208. // progressionSum()
  209. function progressionSum() {
  210. let n = Number(prompt("До какого числа продолжать прогрессию?",""));
  211. let sum = 0;
  212. for (let i = 1; i <= n; i += 3) {
  213. sum += i;
  214. }
  215. console.log(sum);
  216. }
  217. // chessOneLine()
  218. function chessOneLine() {
  219. let size = +prompt("Введите длинну строки","");
  220. let str = '';
  221. for (let i = 0; i < size; i++) {
  222. if (i % 2 === 0) {
  223. str += ' ';
  224. } else {
  225. str += '#';
  226. }
  227. }
  228. console.log(str);
  229. }
  230. // numbers()
  231. function numbers() {
  232. let str = ''
  233. for (let i=0; i < 10; i++) {
  234. for (let j=0; j < 10; j++) {
  235. str += `${j}`
  236. }
  237. str += '\n'
  238. }
  239. console.log(str)
  240. }
  241. // chess()
  242. function chess() {
  243. let size = +prompt("Введите размер поля","")
  244. let str = ''
  245. let counter = 0
  246. while (counter < size) {
  247. counter++
  248. if (counter % 2 === 0) {
  249. for (let counter2 = 0; counter2 < size; counter2++) {
  250. if (counter2 % 2 === 0) {
  251. str += '.'
  252. } else {
  253. str += '#'
  254. }
  255. }
  256. str += '\n'
  257. } else {
  258. for (let counter3 = 0; counter3 < size; counter3++) {
  259. if (counter3 % 2 === 0) {
  260. str += '#'
  261. } else {
  262. str += '.'
  263. }
  264. }
  265. str += '\n'
  266. }
  267. }
  268. console.log(str)
  269. }
  270. // cubes()
  271. function cubes() {
  272. let arr = []
  273. let inputNumber = +prompt("Введите длинну массива","")
  274. let i = 0
  275. while (i < inputNumber) {
  276. let element = (i ** 3)
  277. arr.push(element)
  278. i++
  279. }
  280. console.log(arr)
  281. }
  282. // multiplyTable()
  283. function multiplyTable() {
  284. let arr = []
  285. for (let i=0; i < 10; i++) {
  286. arr[i] = []
  287. for (let j=0; j < 10; j++) {
  288. arr[i].push(i*j)
  289. }
  290. }
  291. console.log(arr)
  292. return arr
  293. }
  294. // matrixToHtmlTable()
  295. function matrixToHtmlTable() {
  296. let inputArr = multiplyTable()
  297. let str = '<table>'
  298. for (let i = 0; i < inputArr.length; i++) {
  299. str += '<tr>'
  300. for (let j = 0; j < inputArr[i].length; j++) {
  301. str += `<td>${inputArr[i][j]}</td>`
  302. }
  303. str += '</tr>'
  304. }
  305. str += '</table>'
  306. document.write(str)
  307. }
  308. // triangle()
  309. function triangle() {
  310. let height = +prompt("Введите высоту треугольника","")
  311. let width = (height * 2 - 1)
  312. let arr = []
  313. let str = ''
  314. for (let colCounter = 0; colCounter < width; colCounter++) {
  315. arr[colCounter] = []
  316. for (rowCounter = 0; rowCounter < height; rowCounter++) {
  317. if (colCounter < height) {
  318. if (rowCounter <= colCounter) {
  319. arr[colCounter].push('#')
  320. } else {
  321. arr[colCounter].push('.')
  322. }
  323. } else {
  324. if (rowCounter < (width - colCounter)) {
  325. arr[colCounter].push('#')
  326. } else {
  327. arr[colCounter].push('.')
  328. }
  329. }
  330. }
  331. }
  332. // console.log(arr)
  333. for (let j = 1; j <= arr[0].length; j++) {
  334. for (let i = 0; i < arr.length; i++) {
  335. str += `${arr[i][arr[i].length - j]}`
  336. }
  337. str += '\n'
  338. }
  339. console.log(str)
  340. }
  341. // fortuneTeller()
  342. function fortuneTeller() {
  343. let history = [1,1,1,1]
  344. let predictArray = [[[[],[]],[[],[]]],[[[],[]],[[],[]]]]
  345. let loop = true;
  346. while (loop) {
  347. let compOutput = null
  348. if (predictArray[history[0]][history[1]][history[2]][history[3]] || predictArray[history[0]][history[1]][history[2]][history[3]] === 0) {
  349. compOutput = predictArray[history[0]][history[1]][history[2]][history[3]]
  350. console.log(compOutput)
  351. } else {
  352. compOutput = Math.floor(Math.random()*2)
  353. console.log(compOutput)
  354. }
  355. let userInput = +confirm('Нажмите ОК, если выбираете 1\nНажмите Отмена, если выбираете 0')
  356. // console.log(userInput)
  357. predictArray[history[0]][history[1]][history[2]][history[3]] = userInput
  358. // console.log(predictArray)
  359. history.push(userInput)
  360. history.shift()
  361. // console.log(history)
  362. loop = confirm('Еще?')
  363. }
  364. }