script.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Task sort
  2. const persons = [
  3. {name: "Иван", age: 17},
  4. {name: "Мария", age: 35},
  5. {name: "Алексей", age: 73},
  6. {name: "Яков", age: 2},
  7. ]
  8. function sort(obj, param, direction){
  9. return {...obj.sort(byField(param, direction))}
  10. }
  11. function byField(param, direction) {
  12. if (direction === undefined){
  13. return ((a, b) => a[param] > b[param] ? 1 : -1);
  14. }
  15. return direction ? ((a, b) => a[param] > b[param] ? 1 : -1) : (a, b) => a[param] < b[param] ? 1 : -1;
  16. }
  17. console.log(sort(persons, "age"));
  18. console.log(sort(persons, "name", false));
  19. console.log(sort(persons, "name", true));
  20. // Task array map
  21. const arr = ["1", {}, null, undefined, "500", 700];
  22. function doNumber(array){
  23. return array.map((elem) => Number(elem) ? Number(elem) : elem);
  24. }
  25. console.log(doNumber(arr))
  26. // Task array reduce
  27. const arr2 = ["0", 5, 3, "string", null];
  28. function multiply(arr){
  29. return arr.reduce((prevElem ,elem )=>{
  30. return typeof elem === "number" ? prevElem * elem : prevElem;
  31. }, 1)
  32. }
  33. console.log(multiply(arr2));
  34. // Task object filter
  35. const phone = {
  36. brand: "meizu",
  37. model: "m2",
  38. ram: 2,
  39. color: "black",
  40. };
  41. function filter(obj, callBack){
  42. const newObj = {};
  43. for(let key in obj){
  44. if(callBack(key, obj[key])){
  45. newObj[key] = obj[key];
  46. }
  47. }
  48. return newObj;
  49. }
  50. console.log(filter(phone,(key,value) => key == "color" || value == 2))
  51. // Task object map
  52. const man = {
  53. name : "Ivan",
  54. age : "17",
  55. }
  56. function map(obj, callBack){
  57. let res = {};
  58. for (let key in obj){
  59. res = {...res, ...callBack(key,obj[key])}
  60. }
  61. return res
  62. }
  63. console.log(map({name: "Иван", age: 17},function(key,value){
  64. var result = {};
  65. result[key+"_"] = value + "$";
  66. return result;
  67. }))
  68. // Task Рекурсия Sum
  69. function sum(n){
  70. if (n === 0){
  71. return 0
  72. }else{
  73. return n + sum(n-1);
  74. }
  75. }
  76. console.log(sum(10));
  77. // Task HTML tree
  78. const someTree = {
  79. tagName: "table",
  80. subTags: [
  81. {
  82. tagName: "tr",
  83. subTags: [
  84. {
  85. tagName: "td",
  86. text: "some text",
  87. },
  88. {
  89. tagName: "td",
  90. text: "some text 2",
  91. },
  92. ]
  93. },
  94. ],
  95. attrs:
  96. {
  97. border: 1,
  98. },
  99. };
  100. let domStructure = '';
  101. fillHtml(someTree);
  102. document.write(domStructure)
  103. function fillHtml(objTree) {
  104. domStructure += `<${objTree.tagName} ${(objTree.attrs) ? Object.keys(objTree.attrs)[0] + "=" + Object.values(objTree.attrs)[0] : ""}>`;
  105. domStructure += `${objTree.text ? objTree.text : ""}`;
  106. if(objTree.subTags && (objTree.subTags.length >0)){
  107. for (let i = 0; i < objTree.subTags.length; i++) {
  108. fillHtml(objTree.subTags[i]);
  109. }
  110. }
  111. domStructure += `</${objTree.tagName}>`
  112. }