script.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //sort
  2. let persons = [
  3. {name: "Иван", age: 17},
  4. {name: "Мария", age: 35},
  5. {name: "Алексей", age: 73},
  6. {name: "Яков", age: 12},
  7. {name: "Яков", age: 1132},
  8. {name: "Яков", age: 22},
  9. {name: "Яков", age: 142},
  10. ]
  11. function arrsort(arr, prop, incline=true) {
  12. if (arr.length < 2) {
  13. return arr
  14. }
  15. let mid = Math.floor(arr.length / 2)
  16. let equ = arr.filter(value => value[prop] === arr[mid][prop])
  17. let more = incline? arr.filter(value => value[prop] > arr[mid][prop]) :
  18. (arr.filter(value => value[prop] < arr[mid][prop])).reverse()
  19. let less = incline? arr.filter(value => value[prop] < arr[mid][prop]) :
  20. (arr.filter(value => value[prop] > arr[mid][prop])).reverse()
  21. return [...arrsort(less, prop, incline), ...equ, ...arrsort(more, prop, incline)]
  22. }
  23. console.log(arrsort(persons, 'age', false))
  24. //array map
  25. let mapped = ["1", {}, null, undefined, "500", 700].map((elem) => {
  26. return isNaN(elem) || typeof elem === 'object'? elem : +elem
  27. })
  28. console.log(mapped)
  29. //array reduce
  30. let reduced = ["0", 5, 3, "string", null, 2, 100].reduce((prev, curr)=> {
  31. let sum = typeof prev === 'number'? prev : 1
  32. let isPresent = false;
  33. (function() {
  34. isPresent = typeof prev === 'number' || typeof curr === 'number'? true : false
  35. if(typeof curr === 'number') {
  36. sum = sum * curr
  37. }
  38. })();
  39. return isPresent? sum : 0
  40. })
  41. console.log(reduced)
  42. //object filter
  43. let phone = {
  44. brand: "meizu",
  45. model: "m2",
  46. ram: 2,
  47. color: "black",
  48. };
  49. function objectFilter(obj, f) {
  50. let filtered = {}
  51. for(let key in obj) {
  52. if(f(key, obj[key])) {
  53. filtered[key] = obj[key]
  54. }
  55. }
  56. return filtered
  57. }
  58. let filtered = objectFilter(phone, (key, value) => key === 'model' || value === 'black')
  59. console.log(filtered)
  60. //object map
  61. function objectMap(object, f) {
  62. let mapped = {}
  63. for(let key in object) {
  64. let temp = f(key, object[key])
  65. for(let prop in temp) {
  66. console.log('biba',prop, temp[prop])
  67. mapped[prop] = temp[prop]
  68. }
  69. }
  70. return mapped
  71. }
  72. console.log(objectMap({name: "Иван", age: 17}, function(key,value){
  73. var result = {};
  74. result[key+"_"] = value + "$";
  75. console.log('ras', result)
  76. return result;
  77. }))
  78. //sum
  79. function sum(initial, step, members) {
  80. var res = initial
  81. var memb = members
  82. if(memb === 0) {
  83. return res
  84. } else {
  85. return sum(res + step, step, memb-1)
  86. }
  87. }
  88. console.log('sum', sum(5, 3, 10))
  89. //HTML Tree
  90. // blue belt
  91. class Markup {
  92. constructor(tagName, [...nestedTags]=[], {...attrs}={}, text='') {
  93. this.tagName = tagName,
  94. this.attrs = attrs
  95. this.text = text
  96. this.nestedTags = nestedTags
  97. }
  98. }
  99. let md3 = new Markup(
  100. 'table',
  101. [
  102. new Markup(
  103. 'tr',
  104. [
  105. new Markup(
  106. 'td',
  107. undefined,
  108. {disabled:true},
  109. 'some text'
  110. ),
  111. new Markup(
  112. 'td',
  113. undefined,
  114. undefined,
  115. 'some text2'
  116. )
  117. ]
  118. ),
  119. new Markup(
  120. 'tr',
  121. [
  122. new Markup(
  123. 'td',
  124. [
  125. new Markup(
  126. 'div',
  127. [
  128. new Markup(
  129. 'p',
  130. undefined,
  131. {disabled:true},
  132. 'some textp'
  133. ),
  134. new Markup(
  135. 'p',
  136. undefined,
  137. {style:'font-weight:800;color:red'},
  138. 'some textp'
  139. )
  140. ]
  141. )
  142. ],
  143. {style:'background-color:purple;color:white;'},
  144. 'some text div'
  145. ),
  146. new Markup(
  147. 'td',
  148. undefined,
  149. {class:'someclass'},
  150. 'some text4'
  151. )
  152. ]
  153. )
  154. ],
  155. {border: 1, style:"color:'red';"}
  156. )
  157. function stringifyMdObject(Md_obj) {
  158. let str = ''
  159. for(let key in Md_obj) {
  160. if(key === 'tagName') {
  161. str += `<${Md_obj[key]}`
  162. if(Md_obj.attrs.length !== 0) {
  163. continue;
  164. } else {
  165. str += `>`
  166. }
  167. }
  168. if(key === 'nestedTags') {
  169. for(let i = 0; i < Md_obj[key].length; i++) {
  170. str += stringifyMdObject(Md_obj[key][i])
  171. }
  172. }
  173. if(key === 'attrs' && Md_obj[key].length !== 0) {
  174. for(let i in Md_obj.attrs) {
  175. str += ` ${i}="${Md_obj.attrs[i]}"`
  176. }
  177. str += `>`
  178. }
  179. }
  180. str += `${Md_obj.text}`
  181. str += `</${Md_obj.tagName}>`
  182. return str
  183. }
  184. document.write(stringifyMdObject(new Markup('h2',[new Markup('span',undefined,{style:'color:blue;'},'Blue ')],undefined,'belt')))
  185. document.write(stringifyMdObject(md3))