script.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* ЗАДАНИЕ makeProfileTimer */
  2. function makeProfileTimer() {
  3. let start = performance.now();
  4. return () => performance.now() - start;
  5. }
  6. let timer = makeProfileTimer();
  7. alert('Замеряем время работы этого alert');
  8. alert(timer());
  9. /* ЗАДАНИЕ makeSaver */
  10. function makeSaver(func){
  11. let saver = func();
  12. return () => saver;
  13. }
  14. let saver = makeSaver(Math.random);
  15. let value1 = saver()
  16. let value2 = saver()
  17. console.log(value1 === value2);
  18. let saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random()*6)])
  19. let value3 = saver2()
  20. let value4 = saver2()
  21. console.log(value3 === value4);
  22. /* ЗАДАНИЕ Final Countdown */
  23. ((n=5) => {
  24. setTimeout(function run() {
  25. console.log(n--);
  26. let stop = setTimeout(run, 1000);
  27. if(n===0) {clearTimeout(stop); console.log("поехали!")}
  28. }, 1000)
  29. })();
  30. /* ЗАДАНИЕ myBind */
  31. function myBind(func, context, args){
  32. return (...undArgs) => {
  33. let counter = 0;
  34. args.forEach((item, index) => {
  35. (item === undefined) && args.splice(index, 1, undArgs[counter++]);
  36. })
  37. return func.apply(context, args);
  38. }
  39. }
  40. let pow5 = myBind(Math.pow, Math, [undefined, 5]);
  41. let cube = myBind(Math.pow, Math, [undefined, 3])
  42. console.log(pow5(2));
  43. console.log(cube(3));
  44. let chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5,undefined, 8,undefined, 9]);
  45. console.log(chessMin(-1,-5,3,15));
  46. let zeroPrompt = myBind(prompt, window, [undefined, "0"]);
  47. let someNumber = zeroPrompt("Введите число");
  48. console.log(someNumber);
  49. console.log(myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a','c','d'));
  50. // Переделанная таблицы умножения
  51. function multiplicationTable() {
  52. let table = document.createElement('table');
  53. table.style.fontFamily = 'sans-serif';
  54. let arrNum = [];
  55. for (let i = 1; i <= 10; i++) {
  56. arrNum[i] = [];
  57. for (let j = 1; j <= 10; j++) {
  58. arrNum[i][j-1] = i * j;
  59. }
  60. }
  61. for (let row = 1; row <= arrNum.length-1; row++) {
  62. let tr = document.createElement('tr');
  63. for (let col = 0; col < arrNum.length-1; col++) {
  64. let td = document.createElement('td');
  65. td.innerText = arrNum[row][col];
  66. td.onmouseout = function(event){
  67. this.style.backgroundColor = '';
  68. Array.from(this.parentElement.children).forEach(item => item.style.backgroundColor = '');
  69. colTable(this.cellIndex, '');
  70. }
  71. td.onmouseover = function(event){
  72. Array.from(this.parentElement.children).forEach(item => item.style.backgroundColor = '#aaa');
  73. colTable(this.cellIndex, '#aaa');
  74. this.style.backgroundColor = '#6d75cb';
  75. }
  76. tr.append(td);
  77. }
  78. for (let cell of tr.cells) {
  79. cell.style.border = '2px solid #000';
  80. cell.style.padding = '10px';
  81. cell.style.textAlign = 'center';
  82. cell.classList.add('td');
  83. }
  84. table.append(tr);
  85. }
  86. document.body.append(table);
  87. function colTable(index, str){
  88. for (let row of document.querySelector('table').rows) {
  89. row.cells[index].style.backgroundColor = str;
  90. }
  91. }
  92. } // Таблица умножения
  93. multiplicationTable();