main.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. //Literals
  2. /*
  3. {
  4. const i = {
  5. name: "Bohdan",
  6. surname: "Baberya",
  7. age: 21,
  8. }
  9. console.log(i.name, i.age)
  10. alert(i.name + " " + i.age)
  11. }
  12. //Literals expand
  13. {
  14. const i = {
  15. name: "Bohdan",
  16. surname: "Baberya",
  17. age: 21,
  18. }
  19. i[prompt("write key")] = prompt('')
  20. i[prompt("write key")] = prompt('')
  21. console.log(i)
  22. }
  23. //Literals copy
  24. {
  25. const i = {
  26. name: "Bohdan",
  27. surname: "Baberya",
  28. age: 21,
  29. }
  30. i[prompt("write key")] = prompt('')
  31. i[prompt("write key")] = prompt('')
  32. const iCopy = i
  33. iCopy[prompt()] = prompt()
  34. console.log(i)
  35. }
  36. //Html tree
  37. {
  38. var body = {
  39. tagName: "body",
  40. children: [
  41. {
  42. tagName: "div",
  43. children: [
  44. {
  45. tagName: 'span',
  46. children: ["Enter a data please:"],
  47. },
  48. {
  49. tagName: 'br/',
  50. },
  51. {
  52. tagName: 'input',
  53. attrs:
  54. {
  55. type: 'text',
  56. id: 'name',
  57. }
  58. },
  59. {
  60. tagName: 'input',
  61. attrs:
  62. {
  63. type: 'text',
  64. id: 'surname',
  65. }
  66. }
  67. ]
  68. },
  69. {
  70. tagName: "div",
  71. children: [
  72. {
  73. tagName: 'button',
  74. attrs:
  75. {
  76. id: 'ok',
  77. },
  78. children: ['OK'],
  79. },
  80. {
  81. tagName: 'button',
  82. attrs:
  83. {
  84. id: 'cancel',
  85. },
  86. children: ['Cancel'],
  87. },
  88. ]
  89. }
  90. ]
  91. }
  92. console.log(body.children[0].children[0].children)
  93. console.log(body.children[0].children[3].attrs.id)
  94. }
  95. //Parent
  96. {
  97. var body = {
  98. tagName: "body",
  99. children: [
  100. {
  101. tagName: "div",
  102. parent: body,
  103. children: [
  104. {
  105. tagName: 'span',
  106. parent: ['body.children[0]'],
  107. children: ["Enter a data please:"],
  108. },
  109. {
  110. tagName: 'br',
  111. parent: ['body.children[0]'],
  112. },
  113. {
  114. tagName: 'input',
  115. parent: ['body.children[0]'],
  116. attrs:
  117. {
  118. type: 'text',
  119. id: 'name',
  120. }
  121. },
  122. {
  123. tagName: 'input',
  124. parent: ['body.children[0]'],
  125. attrs:
  126. {
  127. type: 'text',
  128. id: 'surname',
  129. }
  130. }
  131. ]
  132. },
  133. {
  134. tagName: "div",
  135. parent: body,
  136. children: [
  137. {
  138. tagName: 'button',
  139. parent: ['body.children[1]'],
  140. attrs:
  141. {
  142. id: 'ok',
  143. },
  144. children: ['OK'],
  145. },
  146. {
  147. tagName: 'button',
  148. parent: ['body.children[1]'],
  149. attrs:
  150. {
  151. id: 'cancel',
  152. },
  153. children: ['Cancel'],
  154. },
  155. ]
  156. }
  157. ]
  158. }
  159. // console.log(body.children[0].children[0].children)
  160. //console.log(body.children[0].children[3].attrs.id)
  161. console.log(body.children[1].children[1])
  162. }
  163. //destruct array
  164. {
  165. let arr = [1,2,3,4,5, "a", "b", "c"]
  166. const [odd1, even1, odd2, even2, odd3, ...someLetters] = arr
  167. console.log(odd1)
  168. console.log(someLetters)
  169. }
  170. //destruct string
  171. {
  172. let arr = [1, "abc"]
  173. const [number, [s1, s2, s3]] = arr
  174. console.log(s2)
  175. }
  176. //destruct 2
  177. {
  178. let obj = {name: 'Ivan',
  179. surname: 'Petrov',
  180. children: [{name: 'Maria'}, {name: 'Nikolay'}]}
  181. const {children: [{name:name1}, {name:name2}]} = obj
  182. console.log(name1)
  183. console.log(name2)
  184. }
  185. //destruct 3
  186. let arr = [1,2,3,4, 5,6,7,10]
  187. const [a,b, ...{length: length}] = arr
  188. console.log(a, length)
  189. //Change OK
  190. {
  191. var body = {
  192. tagName: "body",
  193. children: [
  194. {
  195. tagName: "div",
  196. children: [
  197. {
  198. tagName: 'span',
  199. children: ["Enter a data please:"],
  200. },
  201. {
  202. tagName: 'br/',
  203. },
  204. {
  205. tagName: 'input',
  206. attrs:
  207. {
  208. type: 'text',
  209. id: 'name',
  210. }
  211. },
  212. {
  213. tagName: 'input',
  214. attrs:
  215. {
  216. type: 'text',
  217. id: 'surname',
  218. }
  219. }
  220. ]
  221. },
  222. {
  223. tagName: "div",
  224. children: [
  225. {
  226. tagName: 'button',
  227. attrs:
  228. {
  229. id: 'ok',
  230. },
  231. children: ['OK'],
  232. },
  233. {
  234. tagName: 'button',
  235. attrs:
  236. {
  237. id: 'cancel',
  238. },
  239. children: ['Cancel'],
  240. },
  241. ]
  242. }
  243. ]
  244. }
  245. // console.log(body.children[0].children[0].children)
  246. //console.log(body.children[0].children[3].attrs.id)
  247. body.children[1].children[0].attrs[prompt()] = prompt()
  248. console.log(body)
  249. }
  250. //Destructure
  251. {
  252. var body = {
  253. tagName: "body",
  254. children: [
  255. {
  256. tagName: "div",
  257. children: [
  258. {
  259. tagName: 'span',
  260. children: ["Enter a data please:"],
  261. },
  262. {
  263. tagName: 'br/',
  264. },
  265. {
  266. tagName: 'input',
  267. attrs:
  268. {
  269. type: 'text',
  270. id: 'name',
  271. }
  272. },
  273. {
  274. tagName: 'input',
  275. attrs:
  276. {
  277. type: 'text',
  278. id: 'surname',
  279. }
  280. }
  281. ]
  282. },
  283. {
  284. tagName: "div",
  285. children: [
  286. {
  287. tagName: 'button',
  288. attrs:
  289. {
  290. id: 'ok',
  291. },
  292. children: ['OK'],
  293. },
  294. {
  295. tagName: 'button',
  296. attrs:
  297. {
  298. id: 'cancel',
  299. },
  300. children: ['Cancel'],
  301. },
  302. ]
  303. }
  304. ]
  305. }*/
  306. //console.log(body.children[0].children[0].children[0])
  307. //console.log(body.children[1].children[1].children[0])
  308. //console.log(body.children[0].children[3].attrs.id)
  309. /* OR */
  310. /* const {children: [{children: [{children},{},{},{attrs: {id}}]},{children: [{}, {children: [button]}]}]} = body
  311. console.log(children,id,button)
  312. alert(children + " " + id + " " + button)
  313. }
  314. //Copy delete
  315. {
  316. const i = {
  317. name: "Bohdan",
  318. surname: "Baberya",
  319. age: 21,
  320. }
  321. const {[prompt("Write key for delete: ")]:del, ...other} = i
  322. console.log(other)
  323. }
  324. //Currency real rate
  325. {
  326. let firstValue = prompt("write your value").toUpperCase()
  327. let secondValue = prompt("write your second value").toUpperCase()
  328. let amount = +prompt("Your amount value")
  329. fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
  330. .then(data => {
  331. let result, rate
  332. rate = data.rates[secondValue]
  333. result =amount * rate
  334. console.log(data)
  335. console.log(result)
  336. alert(result)
  337. })
  338. }
  339. // Currency drop down
  340. {
  341. fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
  342. .then(data => {
  343. console.log(data)
  344. const allRates = Object.keys(data.rates)
  345. console.log(allRates)
  346. let str = "<select>"
  347. for(let all of allRates){
  348. str += `"<option>" ${all} "</option>"`
  349. }
  350. str += "</select>"
  351. document.write(str)
  352. })
  353. }
  354. //Currency table
  355. {
  356. fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
  357. .then(data => {
  358. const key = 0
  359. const value = 1
  360. const arr = Object.entries( data.rates )
  361. let str = "<table border=1px>"
  362. str += "<tr>" + "<td>" + "</td>"
  363. for (let i = 0; i<arr.length; i++){
  364. str += `<td> ${arr[i][key]} </td>`
  365. }
  366. str += "</tr>"
  367. for (let i = 0; i<arr.length; i++){
  368. str += `<tr><td> ${arr[i][key]} </td>`
  369. rate1 = arr[i][value]
  370. for(let j=0; j<arr.length; j++){
  371. rate2 = arr[j][value]
  372. crossRate = rate2 / rate1
  373. str += `<td> ${crossRate.toFixed(2)} </td>`
  374. }
  375. }
  376. str += "</tr>"
  377. str += "</table>"
  378. console.log(data)
  379. document.write(str)
  380. })
  381. }
  382. */
  383. // Form
  384. {
  385. const car = {
  386. "Name":"chevrolet chevelle malibu",
  387. "Cylinders":8,
  388. "Displacement":307,
  389. "Horsepower":130,
  390. "Weight_in_lbs":3504,
  391. "Origin":"USA",
  392. "in_production": false
  393. }
  394. let str = "<form>"
  395. for ( const [key,value] of Object.entries(car)){
  396. if (typeof(value) === "string"){
  397. str += `<label>${key}: <input type="text" value="${value}" /> </label> `
  398. }
  399. if(typeof(value) === "number"){
  400. str += `<label>${key}: <input type="number" value="${value}" /> </label> `
  401. }
  402. if(typeof(value) === "boolean"){
  403. str += `<label>${key}: <input type="checkbox" value="${value}" /> </label> `
  404. }
  405. }
  406. str += "</form>"
  407. document.write(str)
  408. console.log(str)
  409. }
  410. /*
  411. //Table
  412. const persons =
  413. [
  414. {
  415. "Name":"chevrolet chevelle malibu",
  416. "Cylinders":8,
  417. "Displacement":307,
  418. "Horsepower":130,
  419. "Weight_in_lbs":3504,
  420. "Origin":"USA"
  421. },
  422. {
  423. "Name":"buick skylark 320",
  424. "Miles_per_Gallon":15,
  425. "Cylinders":8,
  426. "Displacement":350,
  427. "Horsepower":165,
  428. "Weight_in_lbs":3693,
  429. "Acceleration":11.5,
  430. "Year":"1970-01-01",
  431. },
  432. {
  433. "Miles_per_Gallon":18,
  434. "Cylinders":8,
  435. "Displacement":318,
  436. "Horsepower":150,
  437. "Weight_in_lbs":3436,
  438. "Year":"1970-01-01",
  439. "Origin":"USA"
  440. },
  441. {
  442. "Name":"amc rebel sst",
  443. "Miles_per_Gallon":16,
  444. "Cylinders":8,
  445. "Displacement":304,
  446. "Horsepower":150,
  447. "Year":"1970-01-01",
  448. "Origin":"USA"
  449. },
  450. ]
  451. let i = 0
  452. let arrKeys = []
  453. for (let key of persons){
  454. arrKeys += Object.keys(persons[i]) + ","
  455. i++
  456. }
  457. arrKeys = arrKeys.split(",").slice(0,-1)
  458. const arrFilter = (x) => {
  459. return x.filter((el1,el2) => x.indexOf(el1) === el2 )
  460. }
  461. arrKeys = arrFilter(arrKeys)
  462. let arrValue =[]
  463. for (let arrVal of persons){
  464. arrValue.push(arrKeys.map(y=>arrVal[y] === undefined? arrVal[y]="-" : arrVal[y]))
  465. }
  466. arrValue.unshift(arrKeys)
  467. let str = `<table border="1">`
  468. for( let firstString of arrValue){
  469. str += `<tr>`
  470. for(let otherString of firstString){
  471. str += `<td>${otherString}</td>`
  472. }
  473. str += " </tr>"
  474. }
  475. str += "</table>"
  476. document.write(str)
  477. */