index.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Функции 2</title>
  6. </head>
  7. <body>
  8. <script>
  9. //---------sort
  10. /*let persons=[
  11. {name:'Ivan',age:17},
  12. {name:'Maria',age:35},
  13. {name:'Aleksei',age:73},
  14. {name:'Iykov',age:12}
  15. ];
  16. function sort(arr,field,bool=true){
  17. if(bool==false){
  18. arr.sort((a,b)=>a[field]>b[field]?-1:1);
  19. }
  20. if(bool==true){
  21. arr.sort((a,b)=>a[field]>b[field]?1:-1);
  22. }
  23. }
  24. sort(persons,'age');
  25. console.log(persons);*/
  26. /*so(persons,'name',false);
  27. console.log(persons);*/
  28. //---------array map (done)
  29. /*let arr=['1',{},null,undefined,'500',700];
  30. let arr2=arr.map(function(x){
  31. if(x===String(x)){
  32. return Number(x);
  33. }
  34. return x;
  35. })
  36. console.log(arr2);
  37. let arr2 = function mAp(arr){
  38. let arr2=[];
  39. for(let num of arr){
  40. if(num===String(num)){
  41. arr2.push(Number(num));
  42. }else{
  43. arr2.push(num);
  44. }
  45. }
  46. return arr2;
  47. }
  48. console.log(arr2(arr))*/
  49. //---------array reduce (done)
  50. /*let arr=['0',5,3,'string',null];
  51. let num=arr.reduce((res,num)=>{
  52. if(num===Number(num)){
  53. res*=num;
  54. }
  55. return res;
  56. },1);
  57. console.log(num); //15
  58. let num2=function(arr){
  59. let res=1;
  60. for(let i of arr){
  61. if(i===Number(i)){
  62. res*=i;
  63. }
  64. }
  65. return res;
  66. }
  67. console.log(num2(arr)); //15*/
  68. //----------object filter
  69. /*let phone={
  70. brand:'meizu',
  71. model:'m2',
  72. ram:2,
  73. color:'black'
  74. };
  75. function filter(obj,cb){
  76. return Object.entries(obj).filter(e => cb(...e)).reduce((a,[b, c] = b) => (a[b] = c, a),{});
  77. }
  78. console.log(filter(phone,(key,value)=>key=='color'|| value==2));*/
  79. //---------object map
  80. /*function map(obj,cb){
  81. let obj2 = {};
  82. for(var key in obj){
  83. Object.assign(obj2,cb(key, obj[key]));
  84. }
  85. return obj2;
  86. }
  87. console.log(map({name:"Ivan",age:17},function(key,value){
  88. let result={};
  89. result[key+'_']=value+'$';
  90. return result;
  91. }));*/
  92. //---------sum (done)
  93. /*function sum(n){
  94. if(n==1){
  95. return n;
  96. }else{
  97. return n+sum(n-1);
  98. }
  99. }
  100. console.log(sum(10));*/
  101. //----------HTML Tree (blue belt - recursive) (done)
  102. /*var someTree = {
  103. tagName: "table",
  104. subTags: [
  105. {
  106. tagName: "tr",
  107. subTags: [
  108. {
  109. tagName: "td",
  110. text: "some text",
  111. },
  112. {
  113. tagName: "td",
  114. text: "some text 2",
  115. }
  116. ]
  117. }
  118. ],
  119. attrs: {
  120. border: 1,
  121. },
  122. }
  123. let str='';
  124. function createTree(obj){
  125. for(let item in obj){
  126. if(item=='tagName'){
  127. if(obj[item]=='table'){
  128. let [key] = Object.keys(someTree['attrs']);
  129. let [value]= Object.values(someTree['attrs']);
  130. str+=`<${someTree['tagName']} ${key}=${value}>`;
  131. }else{
  132. str+=`<${obj['tagName']}>`;
  133. }
  134. }
  135. if(item=='subTags'){
  136. for(let item2 of obj[item]){
  137. createTree(item2);
  138. }
  139. }
  140. if(item=='text'){
  141. str+=`${obj['text']}`;
  142. }
  143. }
  144. }
  145. createTree(someTree);
  146. console.log(str);
  147. document.write(str);*/
  148. </script>
  149. </body>
  150. </html>