script.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //es6
  2. let afooter = document.querySelector("#afooter"),
  3. bfooter = document.querySelector("#bfooter"),
  4. calculeit = document.querySelector("#calculeit"),
  5. output = document.querySelector("#output");
  6. calculeit.addEventListener("click", function(){ calc });
  7. function calc() {
  8. output.value = ((+afooter.value) + (+bfooter.value));
  9. };
  10. afooter.oninput = calc;
  11. bfooter.oninput = calc;
  12. let table = document.createElement("table"),
  13. src = document.querySelector("script"),
  14. td = document.getElementsByTagName("td"),
  15. tr = document.getElementsByTagName("tr");
  16. document.body.insertBefore(table, src);
  17. let namb = "";
  18. for (let i = 0; i < 10; i++){
  19. namb += "<tr>"
  20. for (let j = 0; j < 10; j++){
  21. if (i == 0){namb += "<td>" + j + "</td>";}
  22. else {namb += "<td>" + i * (j === 0 ? 1 : j) + "</td>";}
  23. }
  24. namb += "</tr>"
  25. }
  26. table.innerHTML = namb
  27. for (let i = 0; i < 100; i++) {
  28. td[i].addEventListener("mouseover", function(){
  29. for (let x = 0; x < 10; x++){td[i].parentElement.children[x].style.backgroundColor = "#98f9d2";}
  30. for (let y = 0; y < 100; y++){if (td[i].cellIndex === td[y].cellIndex)
  31. {td[y].style.backgroundColor = "#98f9d2"
  32. };
  33. };
  34. });
  35. td[i].addEventListener("mouseout", function(){
  36. for (let x = 0; x < 10; x++) {td[i].parentElement.children[x].style.backgroundColor = "#b3fcde";};
  37. for (let y = 0; y < 100; y++) {
  38. if (td[i].cellIndex === td[y].cellIndex) {td[y].style.backgroundColor = "#b3fcde";};
  39. };
  40. });
  41. };
  42. //sort
  43. let suv = [
  44. {manufacturer:"Land Rover", model:"Range Rover Sport SVR", horsePower:575},
  45. {manufacturer:"BMW", model:"X5M", horsePower:555},
  46. {manufacturer:"Jeep", model:"grand cherokee trackhawk", horsePower:717},
  47. {manufacturer:"lamborghini", model:"Urus", horsePower:650},
  48. {manufacturer:"MERCEDES-BENZ", model:"GLE 63 S AMG", horsePower:558},
  49. {manufacturer:"Audi", model:"RS Q3", horsePower:310},
  50. {manufacturer:"Tesla", model:"Model X", horsePower:773},
  51. ]
  52. function sortSuv (d,b,c){
  53. if(b==="horsePower"){suv.sort(function(a,b){return a.horsePower - b.horsePower})
  54. };
  55. if(b==="manufacturer"){
  56. if(c===true){
  57. suv.sort(function(a,b){
  58. if(a.manufacturer>b.manufacturer){return 1}
  59. if(a.manufacturer<b.manufacturer){return -1}
  60. return 0
  61. });
  62. }else console.log("false")
  63. }
  64. }
  65. sortSuv(suv,"manufacturer", confirm("sort by company or by horsepower?"))
  66. sortSuv(suv,"horsePower",true);
  67. console.log(suv);
  68. // array map
  69. let arr = ["1", {}, null, undefined, "500", 700];
  70. let arrNumb = arr.map(function(set){
  71. return typeof(set) === 'string' ? +set : set;
  72. });
  73. console.log(arrNumb);
  74. // array reduce
  75. let arrRest = ["0", 5, 3, "string", null];
  76. let result = arrRest.reduce(function(previous,current){
  77. if(typeof(current)==='number'){
  78. previous*=current;
  79. };
  80. return previous;
  81. }, 1 )
  82. console.log(result);
  83. //object filter
  84. var phone = {
  85. brand: "meizu",
  86. model: "m2",
  87. ram: 2,
  88. color: "black",
  89. };
  90. let phoneResult={};
  91. function filter(val,key,obj) {
  92. for(let [keY, vaL] of Object.entries(obj)){
  93. if (keY == key || vaL == val) {
  94. phoneResult[keY] = vaL;
  95. }
  96. }
  97. console.log(phoneResult);
  98. return phoneResult;
  99. }
  100. filter(2, 'color', phone);
  101. //object map
  102. let objectTest = {name: "Иван", age: 17}, result={};
  103. function maps(){
  104. for(let [val,key] of Object.entries(objectTest)){
  105. result[val+"_"]=key+"$";
  106. };
  107. return result;
  108. };
  109. console.log(maps());
  110. //sum
  111. function sum (s){
  112. if (s===1)return 1;
  113. return s + sum(s-1);
  114. }
  115. console.log(sum(28))