script.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //html tree
  2. let obj = {
  3. body: {
  4. tagName: 'body',
  5. attrs: {},
  6. paired: true,
  7. subTags: [
  8. {
  9. tagName: 'div',
  10. attrs: {},
  11. paired: true,
  12. subTags: [
  13. {
  14. tagName: 'span',
  15. attrs: {},
  16. paired: true,
  17. text: 'Enter data please:',
  18. },
  19. {
  20. tagName: 'br',
  21. paired: false
  22. },
  23. {
  24. tagName: 'input',
  25. attrs: {
  26. type: 'text',
  27. id: 'name'
  28. },
  29. paired: false
  30. },
  31. {
  32. tagName: 'input',
  33. attrs: {
  34. type: 'text',
  35. id: 'surname'
  36. },
  37. paired: false
  38. }
  39. ]
  40. },
  41. {
  42. tagName: 'div',
  43. attrs: {},
  44. paired: true,
  45. subTags: [
  46. {
  47. tagName: 'button',
  48. attrs: {
  49. id: 'ok'
  50. },
  51. paired: true,
  52. text: 'Ok'
  53. },
  54. {
  55. tagName: 'button',
  56. attrs: {
  57. id: 'cancel'
  58. },
  59. paired: true,
  60. text: 'Cancel'
  61. },
  62. ],
  63. },
  64. ]
  65. }
  66. }
  67. console.log(`<button id='cancel'> innerText:`, obj.body.subTags[1].subTags[1].text)
  68. console.log(`<input type='text' id='surname'> id:`, obj.body.subTags[0].subTags[3].attrs.id)
  69. //declarative fields
  70. var notebook = {
  71. brand: prompt('Notebook brand:','') || "HP",
  72. type: prompt('Notebook type:','') || "440 G4",
  73. model: prompt('Notebook model:','') || "Y7Z75EA",
  74. ram: +prompt('Notebook ram:','') || 4,
  75. size: prompt('Notebook size:','') || "14",
  76. weight: +prompt('Notebook weight:','') ||1.8,
  77. resolution: {
  78. width: +prompt('Notebook width:','') || 1920,
  79. height: +prompt('Notebook height','') || 1080,
  80. },
  81. };
  82. console.log(notebook)
  83. var phone = {
  84. brand: prompt('Phone brand:','') || "meizu",
  85. model: prompt('Phone model:','') || "m2",
  86. ram: +prompt('Phone amount of ram:','') || 2,
  87. color: prompt('Phone color','') || "black",
  88. };
  89. console.log(phone)
  90. var person = {
  91. Name: prompt('Person name:','') || 'Donald',
  92. surname: prompt('Person surname:','') || "Trump",
  93. married: confirm('is married?'),
  94. }
  95. console.log(person)
  96. //object links
  97. notebook.owner = phone.owner = person
  98. person.phone = phone
  99. person.notebook = notebook
  100. console.log(phone, notebook)
  101. console.log(person.phone.owner.notebook.owner.phone == person.phone )
  102. //imperative array fill 3
  103. let imperatve = [prompt('input array[0] element','')||0, prompt('input array[1] element','')||1, prompt('input array[2] element','')||2]
  104. console.log(imperatve)
  105. //while confirm
  106. while(!confirm('break the loop?')){}
  107. //array fill
  108. let arrayFill = []
  109. for(let i = 0, str; i < arrayFill.length+1; i++) {
  110. str = prompt('input array element','')
  111. if(!str && str !== '') {
  112. break;
  113. } else {
  114. arrayFill.push(str)
  115. }
  116. }
  117. console.log(arrayFill)
  118. let arrayNoPush = []
  119. for(let i = 0, str2; i < arrayNoPush.length+1; i++) {
  120. str2 = prompt('input arrayNoPush element','')
  121. if(!str2 && str2 !== '') {
  122. break;
  123. } else {
  124. arrayNoPush[arrayNoPush.length] = str2
  125. }
  126. }
  127. console.log(arrayNoPush)
  128. //infinite probability
  129. let trys = 0
  130. while(true) {
  131. trys++
  132. let rand = Math.random()
  133. if(rand > 0.9) {
  134. alert(`Number of iterations: ${trys}`)
  135. break;
  136. }
  137. }
  138. //empty loop
  139. while(!prompt('Input smth and press OK to stop','')){}
  140. //progression sum
  141. let N = 10
  142. let step = 1
  143. let sum = 0
  144. for(let i = 1; i <= N; i++) {
  145. sum = sum + step
  146. step += 3
  147. }
  148. console.log('progression sum:', sum)
  149. //chess one line
  150. let strlen = 11
  151. let str = ''
  152. for(let i = 0; i < strlen; i++) {
  153. i % 2 === 0? str += ' ' : str += '#'
  154. }
  155. console.log(str, str.length)
  156. //numers
  157. let numbers = ''
  158. for(let i = 0; i < 10; i++) {
  159. for(let i = 0; i < 10; i++) {
  160. numbers += `${i}`
  161. }
  162. numbers += '\n'
  163. }
  164. console.log(numbers)
  165. //chess
  166. let height = 3
  167. let width = 12
  168. let chessboard = ''
  169. for(let i = 0; i < height; i++) {
  170. for(let j = 0; j < width; j++) {
  171. i % 2 === 0? (j % 2 === 0? chessboard+='.' : chessboard+='#') : (j % 2 === 0? chessboard +='#' : chessboard+='.')
  172. }
  173. chessboard += '\n'
  174. }
  175. console.log(chessboard)
  176. //cubes
  177. let cubes = new Array(10)
  178. for(let i = 0; i < cubes.length; i++) { cubes[i] = i**3 }
  179. console.log(cubes)
  180. //multiply table
  181. let mulTable = []
  182. for(let i = 0; i < 10; i++) {
  183. let innerArr = []
  184. for(let j = 0; j < 10; j++) {
  185. innerArr.push(i * j)
  186. }
  187. mulTable[i] = innerArr
  188. }
  189. console.log(mulTable[5][6]) //будет 30 (но есть нулевые елементы в массиве)
  190. //matrix to html table
  191. let tableStr = ''
  192. let colors = ['red', 'orange', 'yellow', 'green', 'skyblue', 'blue', 'purple', 'red', 'orange']
  193. let prevColors = []
  194. tableStr += '<table>'
  195. for (let i = 1; i < mulTable.length; i++) {
  196. tableStr += `<tr>`
  197. prevColors.push(colors[i - 1])
  198. for (let j = 1; j < mulTable.length; j++) {
  199. tableStr += `<td
  200. style="text-align: center;
  201. color: white;
  202. text-shadow: 1px 1px 2px black;
  203. background-color: ${j >= prevColors.length? prevColors[i-1] : prevColors[j-1]}";"
  204. >&nbsp${mulTable[i][j]}&nbsp</td>`
  205. }
  206. tableStr += '</tr>'
  207. }
  208. tableStr += '</table>'
  209. document.write(tableStr)
  210. //blue belt
  211. let trigon = ''
  212. for(let i = 6, step = 1; i > 0; i--) {
  213. let repeats = 10
  214. for(let j = 0; j <= repeats; j++) {
  215. if(j === i - 1) {
  216. repeats = repeats - step + 1
  217. for(let k = 0; k < step; k++) {
  218. trigon += '#'
  219. }
  220. } else {
  221. trigon += '.'
  222. }
  223. }
  224. trigon += '\n'
  225. step += 2
  226. }
  227. console.log(trigon)
  228. //black belt
  229. let history = [1, 1, 1, 1]
  230. let predictArray = [
  231. [
  232. [[-1],[-1]],[[-1],[-1]]
  233. ],
  234. [
  235. [[-1],[-1]],[[-1],[-1]]
  236. ]
  237. ]
  238. while(true) {
  239. console.log('my prediction is:',
  240. predictArray[history[0]][history[1]][history[2]][history[3]] >= 0? predictArray[history[0]][history[1]][history[2]][history[3]]:
  241. Math.random() > 0.5? 1 : 0
  242. )
  243. let variant = prompt('input 1 or 0')
  244. if(variant === null) {
  245. break;
  246. } else if(+variant === 1 || +variant === 0) {
  247. predictArray[history[0]][history[1]][history[2]][history[3]] = +variant
  248. history.shift()
  249. history.push(+variant)
  250. }
  251. }