main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Таблица, замыкание
  2. let table = document.createElement('table')
  3. for (let rowIndex = 0; rowIndex <= 10; rowIndex++) {
  4. let tr = document.createElement('tr')
  5. rowIndex % 2 === 0 ? tr.className = "tr-odd" : tr.className = "tr-even"
  6. table.append(tr)
  7. if (rowIndex === 0) {
  8. for (let t = 0; t <= 10; t++) {
  9. let th = document.createElement('th')
  10. th.className = 'th'
  11. th.innerText = t
  12. tr.append(th)
  13. }
  14. } else {
  15. for (let colIndex = 0; colIndex <= 10; colIndex++) {
  16. let td = document.createElement('td')
  17. td.onmousemove = function () {
  18. [...tr.parentElement.children].map(t => t.children[colIndex].style.backgroundColor = '#9e90ff')
  19. td.parentElement.style.backgroundColor = '#9e90ff';
  20. td.style.backgroundColor = 'red'
  21. }
  22. td.onmouseout = function () {
  23. [...tr.parentElement.children].map(t => t.children[colIndex].style.backgroundColor = '')
  24. td.style.backgroundColor = ''
  25. td.parentElement.style.backgroundColor = ''
  26. }
  27. if (colIndex === 0) {
  28. let th = document.createElement('th')
  29. th.className = 'th'
  30. th.innerText = (rowIndex) * (colIndex + 1)
  31. tr.append(th)
  32. } else if (colIndex === rowIndex) {
  33. td.className = 'tdtd'
  34. td.innerText = (rowIndex) * (colIndex)
  35. tr.append(td)
  36. } else {
  37. td.innerText = (rowIndex) * (colIndex)
  38. tr.append(td)
  39. }
  40. }
  41. }
  42. }
  43. root.append(table)
  44. //makeProfileTimer
  45. function makeProfileTimer() {
  46. let t0 = performance.now();
  47. return function () {
  48. let t1 = performance.now();
  49. return t1 - t0
  50. }
  51. }
  52. let timer = makeProfileTimer()
  53. alert('Замеряем время работы этого alert')
  54. alert(timer())
  55. //makeSaver
  56. function makeSaver(param) {
  57. let res;
  58. let flag = false;
  59. return function () {
  60. flag ? res : (flag = true, res = param());
  61. }
  62. };
  63. let saver = makeSaver(Math.random)
  64. let value1 = saver()
  65. let value2 = saver()
  66. console.log(value1 === value2);
  67. let saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random() * 6)])
  68. let value3 = saver2()
  69. let value4 = saver2()
  70. console.log(value3 === value4);
  71. //Final Countdown
  72. (function () {
  73. let seconds = 5
  74. for (let i = 0; i <= seconds; i++) {
  75. setTimeout(() => {
  76. !seconds ? console.log('поехали!') : console.log(seconds--);
  77. }, 1000 * i);
  78. }
  79. }())
  80. //myBind
  81. function myBind(f, method, arr) {
  82. debugger
  83. return function (...params) {
  84. debugger
  85. let countIndex = 0
  86. let newArr = arr.map(i => i !== undefined ? i : params[countIndex++])
  87. return f.apply(method, newArr)
  88. }
  89. }
  90. // var pow5 = myBind(Math.pow, Math, [undefined, 5])
  91. // pow5(2)
  92. // var cube = myBind(Math.pow, Math, [undefined, 3])
  93. // cube(3)
  94. // var chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5, undefined, 8, undefined, 9])
  95. // chessMin(-1, -5, 3, 15)
  96. // var zeroPrompt = myBind(prompt, window, [undefined, "0"])
  97. // var someNumber = zeroPrompt("Введите число")
  98. myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a', 'c', 'd') === 'abcdef'