hw8.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // ES6 ==============================================================================
  2. // a =======================================
  3. let a = (...arguments) => alert(arguments)
  4. // a("Привет")
  5. // a("Как дела?")
  6. // a("Как погода?")
  7. // cube =======================================
  8. let cube =(...arguments) => arguments**=3
  9. cube(17)
  10. // avg2 =======================================
  11. let avg2 = (...arguments) => (arguments[0]+arguments[1])/2
  12. // avg2(7,10)
  13. // sum3 =======================================
  14. let sum3 = (...arguments) => arguments[0]+(arguments[1] ? arguments[1] : 0)+(arguments[2]?arguments[2]:0)
  15. // sum3(1,2,3)
  16. // intRandom =======================================
  17. let intRandom = (...arguments) => (Math.round(Math.random() * ((arguments[1]?arguments[1]:0) - arguments[0] ) + arguments[0]))
  18. // intRandom (0,1)
  19. // greetAll =======================================
  20. let greetAll = (...arguments) => {
  21. for(let i=0; i<arguments.lenght; i++){
  22. arguments+=arguments[i]
  23. }
  24. alert('Hello' +' '+ arguments.join(', '))
  25. }
  26. // greetAll('Kate','Vlad','Marina','Sveta','Andrey','Cat','Superman')
  27. // sum =======================================
  28. let sum = (...arguments) =>{
  29. let result = 0
  30. for(let i=0; i<arguments.length; i++){
  31. result+=arguments[i]
  32. }
  33. return result
  34. }
  35. // sum(10,20,40,100)
  36. // Union =======================================
  37. aChoice = () => a("Привет")
  38. cubeChoice = () => cube(17)
  39. avg2Choice = () => avg2(7,10)
  40. sum3Choice = () => sum3(1,2,3)
  41. intRandomChoice = () => intRandom(0,1)
  42. greetAllchoice = () => greetAll('Kate','Vlad','Marina','Sveta','Andrey','Cat','Superman')
  43. sumChoice = () => sum(10,20,40,100)
  44. let taskChoice
  45. let taskChoiceFunc = () => {
  46. taskChoice= prompt('Введите название задания: \n a - cube - avg2 - sum3 - intRandom - greetAll - sum')
  47. switch(taskChoice?.toLowerCase()){
  48. case 'a': aChoice()
  49. break;
  50. case 'cube': console.log(cubeChoice())
  51. break;
  52. case 'avg2': console.log(avg2Choice())
  53. break;
  54. case 'sum3': console.log(sum3Choice())
  55. break;
  56. case 'intrandom': console.log(intRandomChoice())
  57. break;
  58. case 'greetall': greetAllchoice()
  59. break;
  60. case 'sum': console.log(sumChoice())
  61. }
  62. taskChoice?taskChoiceFunc():false
  63. }
  64. taskChoiceFunc()
  65. // Union declarative =======================================
  66. let taskChoiceTwo = prompt('Введите название задания')
  67. const functionObj = {
  68. 'a': aChoice,
  69. 'cube': cubeChoice,
  70. 'avg2': avg2Choice,
  71. 'sum3': sum3Choice,
  72. 'intrandom': intRandomChoice,
  73. 'greetall': greetAllchoice,
  74. 'sum': sumChoice
  75. }
  76. functionObj[taskChoiceTwo.toLowerCase()]()
  77. // sort ==============================================================================
  78. var persons = [
  79. {name: "Иван", age: 17},
  80. {name: "Мария", age: 35},
  81. {name: "Алексей", age: 73},
  82. {name: "Яков", age: 12},
  83. ]
  84. let sort = (arr,key,Boolean) =>
  85. arr.sort((a,b) =>
  86. Boolean? (a-b) || (a[key]<b[key] ? -1 : 1) : (b-a) || a[key]>b[key] ? -1 : 0
  87. )
  88. console.log(sort(persons,'name',true))
  89. console.log(sort(persons,'age',true))
  90. console.log(sort(persons,'name',false))
  91. console.log(sort(persons,'age',false))
  92. console.log(sort(persons,'name'))
  93. console.log(sort(persons,'age'))
  94. // array map ==============================================================================
  95. let arr = ["1", {}, null, undefined, "500", 700]
  96. let arrMap = arr.map((index) => parseInt(index)?Number(index):index)
  97. console.log(arrMap)
  98. // array reduce ==============================================================================
  99. let arr = ["0", 5, 3, "string", null]
  100. let arrReduce = arr.reduce((index1,index2) => typeof index2 ==='number' ? index1*index2 : index1,1)
  101. console.log(arrReduce)
  102. // object filter ==============================================================================
  103. var phone = {
  104. brand: "meizu",
  105. model: "m2",
  106. ram: 2,
  107. color: "black",
  108. };
  109. const filter =(obj,func) =>{
  110. for(let key in obj){
  111. !func(key,obj[key]) ? delete obj[key] : obj[key]
  112. }
  113. console.log(obj)
  114. }
  115. filter(phone,(key,value) => key == "brand" || value == 2)
  116. // object map ==============================================================================
  117. const map =(obj,func) => {
  118. let result={}
  119. for (let key in obj){
  120. func(key,obj[key]) ? obj : result[key+'_'] = obj[key]+'$'
  121. }
  122. console.log(result)
  123. }
  124. map({name: "Иван", age: 17},function(key,value){
  125. var result = {};
  126. result[key+"_"] = value + "$";
  127. return
  128. }) //должен вернуть {name_: "Иван$", age_: "17$"}
  129. // Sum ==============================================================================
  130. let result = 0
  131. const sum = (start,step,finish) => {
  132. if (finish===0){
  133. return console.log(result)
  134. }
  135. else{
  136. result+=start
  137. sum(start+step,step,finish-1)
  138. }
  139. }
  140. sum(7,5,15)
  141. // HTML Tree ==============================================================================
  142. var someTree = {
  143. tagName: "table", //html tag
  144. subTags: [ //вложенные тэги
  145. {
  146. tagName: "tr",
  147. subTags: [
  148. {
  149. tagName: "td",
  150. text: "some text",
  151. },
  152. {
  153. tagName: "td",
  154. text: "some text 2",
  155. }
  156. ]
  157. }
  158. ],
  159. attrs:
  160. {
  161. border: 1,
  162. },
  163. }
  164. let result = ''
  165. const HTMLtree = (obj) => {
  166. const openTag = '<' + obj.tagName + (obj.attrs? ' ' + Object.keys(obj.attrs) + ' = ' + Object.values(obj.attrs): '') + '>'
  167. const closeTag = '</' + obj.tagName + '>'
  168. const content = obj.text? obj.text: ''
  169. result += openTag + content
  170. if(typeof(obj.subTags) === 'object'){
  171. for (let value of obj.subTags){
  172. HTMLtree(value)
  173. }
  174. }
  175. result += closeTag
  176. return result
  177. }
  178. HTMLtree(someTree)