script.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. //TODO: ES6
  3. // let a = msg => alert(msg);
  4. // let cube = number => number * number * number;
  5. // let avg2 = (a, b) => (a + b) / 2;
  6. // let sum3 = (a = 0, b = 0, c = 0) => a + b + c;
  7. // let intRandom = (a = 0, b = 0) => Math.round(Math.random() * (b - a)) + a;
  8. // let greetAll = (...arguments) => {
  9. // for (let i in arguments) {
  10. // alert(arguments[i])
  11. // }
  12. // }
  13. // let sum = (...arguments) => {
  14. // let sumAll = 0
  15. // for (let i in arguments) {
  16. // sumAll += arguments[i]
  17. // }
  18. // return sumAll;
  19. // }
  20. function sort() {
  21. let persons = [{
  22. name: "Иван",
  23. age: 17
  24. },
  25. {
  26. name: "Мария",
  27. age: 35
  28. },
  29. {
  30. name: "Алексей",
  31. age: 73
  32. },
  33. {
  34. name: "Яков",
  35. age: 12
  36. },
  37. ]
  38. function sort(field, boolean = true) {
  39. if (boolean) {
  40. return (a, b) => a[field] > b[field] ? 1 : -1
  41. } else {
  42. return (a, b) => a[field] < b[field] ? 1 : -1
  43. }
  44. }
  45. console.log(persons.sort(sort('age', false)))
  46. }
  47. // sort()
  48. function arrayMap() {
  49. let arr = ["1", {}, null, undefined, "500", 700];
  50. let newArr = arr.map(item => typeof (item) === "string" ? parseInt(item) : item);
  51. console.log(newArr)
  52. }
  53. // arrayMap()
  54. function arrayReduce() {
  55. let arr = ['0', 5, 3, "string", null];
  56. let result = arr.reduce((sum, currentItem) => {
  57. if (typeof (currentItem) === "number") {
  58. sum *= currentItem;
  59. }
  60. return sum
  61. }, 1)
  62. console.log(result);
  63. }
  64. // arrayReduce()
  65. function objectFilter() {
  66. let phone = {
  67. brand: "meizu",
  68. model: "m2",
  69. ram: 2,
  70. color: "black",
  71. };
  72. function filter(obj, func) {
  73. for (let key in obj) {
  74. console.log(func)
  75. if (!func(key, obj[key])) {
  76. delete obj[key]
  77. }
  78. }
  79. }
  80. filter(phone, (key, value) => key == "color" || value == 2);
  81. console.log(phone)
  82. }
  83. // objectFilter()
  84. //OR
  85. // let newObj = Object.entries(phone)
  86. // .filter(item => item[0] === "color" || item[0] === "ram")
  87. // .map(item => item);
  88. // console.log(newObj)
  89. function objectMap() {
  90. function map(object, callback) {
  91. let obj = {};
  92. for (let key in object) {
  93. Object.assign(obj, callback(key, object[key]));
  94. }
  95. return obj
  96. }
  97. let obj = map({
  98. name: "Иван",
  99. age: 17
  100. }, function (key, value) {
  101. let result = {};
  102. result[key + "_"] = value + "$";
  103. return result;
  104. })
  105. console.log(obj);
  106. }
  107. // objectMap()
  108. function recursion() {
  109. let sum = (firstNumber, step, n) => {
  110. return n == 1 ? firstNumber : firstNumber + sum(firstNumber + step, step, n - 1);
  111. }
  112. }
  113. // recursion()
  114. //TODO: HTML Tree (in process...)