script.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //////html tree
  2. function htmlTree() {
  3. var body = {
  4. tagName: "body",
  5. subTags: [
  6. {
  7. tagName: "div",
  8. subTags: [
  9. {
  10. tagName: "span",
  11. text: "Enter a data please:",
  12. },
  13. {
  14. tagName: "br/",
  15. },
  16. {
  17. tagName: "input",
  18. attrs: {
  19. type: "text",
  20. id: "name"
  21. },
  22. },
  23. {
  24. tagName: "input",
  25. attrs: {
  26. type: "text",
  27. id: "surname"
  28. }
  29. }
  30. ]
  31. },
  32. {
  33. tagName: "div",
  34. subTags: [
  35. {
  36. tagName: "button",
  37. text: "OK",
  38. attrs: {
  39. id: "ok",
  40. }
  41. },
  42. {
  43. tagName: "button",
  44. text: "Cancel",
  45. attrs: {
  46. id: "cancel"
  47. }
  48. }
  49. ]
  50. }
  51. ],
  52. }
  53. alert(body.subTags[1].subTags[1].text)
  54. alert(body.subTags[0].subTags[3].attrs.id)
  55. }
  56. // htmlTree()
  57. //////
  58. //////
  59. /////declarative fields
  60. /////object links
  61. function declarativeFriends() {
  62. var notebook = {
  63. brand: prompt("Name of brand of notebook"),
  64. type: prompt("Number of type in style 'XXX XX'"),
  65. model: prompt("Model code"),
  66. ram: +prompt("Ram"),
  67. size: prompt("Size"),
  68. weight: +prompt("Weight in style X.X"),
  69. resolution: {
  70. width: +prompt("Width"),
  71. height: +prompt("Height"),
  72. },
  73. };
  74. var phone = {
  75. brand: prompt("Name of brand of phone"),
  76. model: prompt("Model code"),
  77. ram: +prompt("Ram"),
  78. color: prompt("What's color?"),
  79. };
  80. var person = {
  81. name: prompt("Write your name?"),
  82. surname: prompt("Write your surname?"),
  83. married: confirm("Are you married? Click 'ok' if answer is 'yes' and 'cancel' if 'no'"),
  84. }
  85. person.laptop = notebook;
  86. person.smartphone = phone;
  87. phone.owner = person;
  88. notebook.owner = person;
  89. alert("Notebook \n owner: " + notebook.owner.name + "\n brand: " + notebook.brand + "\n type: " + notebook.type + "\n model: " + notebook.model + "\n ram: " + notebook.ram + "\n size: " + notebook.size + "\n weight: " + notebook.weight + "\n width: " + notebook.resolution.width + "\n height: " + notebook.resolution.height)
  90. alert("Phone\n owner: " + phone.owner.name + "\n brand: " + phone.brand + "\n model: " + phone.model + "\n ram: " + phone.ram + "\n color: " + phone.color)
  91. alert("Person\n name " + person.name + "\n surname: " + person.surname + "\n married: " + person.married + "\n laptop: " + notebook.brand + "\n smartphone: " + phone.brand)
  92. if (person.smartphone.owner.laptop.owner.smartphone == person.smartphone){
  93. alert ("You're superman")
  94. } else {
  95. alert ("Try again, loser")
  96. }
  97. }
  98. // declarativeFriends()
  99. //////////////
  100. /////imperative array fill 3
  101. function imperativeArray() {
  102. let imperative = [];
  103. imperative[0] = prompt("How old are you?"), imperative[1] = prompt("How old is your father?"), imperative[2] = prompt("How old is your mother?")
  104. alert("So you are " + imperative[0] + " years old. Your father is " + imperative[1] + " and mother is " + imperative[2])
  105. }
  106. // imperativeArray()
  107. ////////
  108. //////
  109. ///while confirm
  110. function whileConfirm(question) {
  111. question = false;
  112. while(question === false) {
  113. question = confirm("Are you're dead?")
  114. }
  115. }
  116. // whileConfirm()
  117. //////////
  118. ////array fill
  119. function arrayFill() {
  120. var array = [];
  121. var once = true;
  122. while(!!once === true || once === "") {
  123. once = prompt("Write something")
  124. array.push(once)
  125. }
  126. alert("The array's length is " + array.length)
  127. }
  128. // arrayFill()
  129. ///////
  130. /////array fill nopush
  131. function arrayFillNopush() {
  132. var i = 0;
  133. var array = [];
  134. var once = prompt("Write something");
  135. while(!!once === true || once === "") {
  136. array[i++] = once;
  137. once = prompt("Write something");
  138. }
  139. alert("The array's length is " + array.length)
  140. }
  141. // arrayFillNopush()
  142. ////infinite probability
  143. function infiniteProbably() {
  144. let haha = true;
  145. let i = 0;
  146. let array = []
  147. debugger
  148. while (haha === true) {
  149. i = Math.random();
  150. array.push(i)
  151. if (i > 0.9) {
  152. break;
  153. }
  154. confirm("And again")
  155. }
  156. alert ("You had " + array.length + " expected output")
  157. }
  158. // infiniteProbably()
  159. //////
  160. ///////empty loop
  161. function emptyLoop() {
  162. var again = null;
  163. while((again = prompt("And again?")) === null) {
  164. }
  165. }
  166. // emptyLoop()
  167. ///////////
  168. //////progression sum
  169. function progressionSum() {
  170. let b = (+prompt("Chose your number"))
  171. let sum = 1;
  172. let a = 1;
  173. for (let i = 1; b > a; i++){
  174. a = a + 3;
  175. if (a > b) {
  176. alert(sum + " but it can have an error in 1-2 count")
  177. return
  178. } else {
  179. sum = sum + a;
  180. }
  181. }
  182. alert (sum)
  183. }
  184. // progressionSum()
  185. //////////////////
  186. ////chess one line - Написала два варианта
  187. function chessOneLine() {
  188. let string = "";
  189. for(let num = +prompt("String length is...");0 < num; num--){
  190. string += "#" + " ";
  191. }
  192. return console.log(string)
  193. }
  194. // chessOneLine()
  195. ///////////////
  196. function chessOneLine2() {
  197. let num = +prompt("String length is...");
  198. let string = "";
  199. for(;num > 0; num = num - 1){
  200. if (num % 2 == 0) {
  201. string += "#";
  202. } else {
  203. string += " ";
  204. }
  205. }
  206. return console.log(string)
  207. }
  208. // chessOneLine2()
  209. ///////
  210. /////numbers
  211. function numberCucle() {
  212. let stringMass = "";
  213. for(let i = 0; i < 10; i++) {
  214. let string = "";
  215. for (let j = 0; j < 10; j++) {
  216. string += j
  217. }
  218. stringMass += string + "\n"
  219. }
  220. console.log(stringMass)
  221. }
  222. // numberCucle()
  223. ///////////////////
  224. //////////
  225. ////chess
  226. function chess(){
  227. let desk = "";
  228. let string = "";
  229. let x = +prompt("Please write a size of horizontal")
  230. let y = +prompt("Please write a size of vertical")
  231. for(let i = 0; i < y; y--){
  232. for(let j = x; j > 0; j--) {
  233. if(y % 2 === 0){
  234. if(j % 2 === 0) {
  235. string += "#";
  236. } else {
  237. string += ".";
  238. }
  239. } else {
  240. if(j % 2 === 0) {
  241. string += ".";
  242. } else {
  243. string += "#";
  244. }
  245. }
  246. }
  247. desk += string + "\n"
  248. string = "";
  249. }
  250. console.log(desk)
  251. }
  252. chess()
  253. ////////////
  254. /////////
  255. //cubes
  256. function cubes() {
  257. var array = [];
  258. var arrayString = "";
  259. array.length = +prompt("And array length is...");
  260. for(let i = 0; i < array.length; i++) {
  261. array[i] = Math.pow(i, 3)
  262. arrayString += array[i] + " "
  263. }
  264. alert(arrayString)
  265. }
  266. // cubes()
  267. ///////////////////////
  268. ///////////////
  269. ////multiply table
  270. function multiplyTable() {
  271. var table = [];
  272. for(let i = 0; i < 10; i++) {
  273. table[i] = [];
  274. for(let j = 0; j < 10; j++){
  275. table[i][j] = ((i) * j)
  276. }
  277. }
  278. console.log(table)
  279. alert(table[5][6])
  280. alert(table[7][2])
  281. }
  282. // multiplyTable()
  283. //////////////////
  284. ///////////
  285. ///matrix to html table
  286. function matrixTable() {
  287. var table = [];
  288. for(let i = 0; i < 10; i++) {
  289. var newDiv = document.createElement("div");
  290. table[i] = [];
  291. for(let j = 0; j < 10; j++){
  292. var newSpan = document.createElement("span");
  293. newSpan.style.display = "inline-block";
  294. newSpan.style.width = "30px"
  295. table[i][j] = ((i) * j);
  296. newSpan.innerHTML = table[i][j];
  297. newDiv.appendChild(newSpan);
  298. }
  299. document.body.append(newDiv);
  300. }
  301. }
  302. // matrixTable()
  303. //////////////////
  304. ////////////////
  305. /////blue belt
  306. function blueBelt() {
  307. var triangle = "";
  308. var string = "";
  309. var sharp = "#";
  310. var dottRep = 5;
  311. for(let j = 0; j < 6; j++) {
  312. var dott = ".";
  313. dott = dott.repeat(dottRep);
  314. string += dott + sharp + dott + "\n";
  315. dottRep = dottRep - 1;
  316. sharp += "##"
  317. }
  318. triangle += string + "\n";
  319. console.log(triangle)
  320. }
  321. // blueBelt()
  322. /////////////////
  323. ////////////////
  324. //blackBelt
  325. var predictArray = [[-1]];
  326. var historyArr = [1, 1, 1, 1];
  327. var myMove = Math.round(Math.random());
  328. function blackBelt() {
  329. var yourMove = +prompt("Your move 0 or 1");
  330. var answer;
  331. if (yourMove === 1 || yourMove === 0) {
  332. if (myMove === yourMove){
  333. answer = confirm("I knew it. Let's try again");
  334. } else {
  335. answer = confirm ("You win. Let's try again");
  336. }
  337. myMove = predictArray[0][0];
  338. predictArray = [[historyArr[0]],[historyArr[1]],[historyArr[2]],[historyArr[3]]];
  339. historyArr.shift();
  340. historyArr.push(yourMove);
  341. if(answer == true) {
  342. return blackBelt()
  343. }
  344. } else {
  345. alert ("Error. Try again")
  346. }
  347. }
  348. // blackBelt();