main.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // ES6
  2. // a
  3. let a = text => alert(text);
  4. // cube
  5. let cube = (a) => a * a * a;
  6. // avg2
  7. let avg2 = (a, b) => (a + b) / 2;
  8. // sum3
  9. let sum3 = (a = 0, b = 0, c = 0) => a + b + c;
  10. // intRandom
  11. let intRandom = (a = 0, b = 0) => Math.round(Math.random() * (b - a)) + a;
  12. // greetAll
  13. let greetAll = (...arguments) => {
  14. for (let i in arguments) {
  15. alert('"' + 'Hello' + ' ' + arguments[i] + '"');
  16. }
  17. }
  18. // sum
  19. let sum = (...arguments) => {
  20. let sum = 0;
  21. for (let i = 0; i < arguments.length;i++) {
  22. sum += arguments[i];
  23. }
  24. return sum;
  25. }
  26. // sort
  27. function byField(field, order) {
  28. let collator = new Intl.Collator(["en", "ru"], {
  29. numeric: true
  30. })
  31. order = order ? 1 : -1;
  32. return function(a, b) {
  33. return order * collator.compare(a[field], b[field]);
  34. }
  35. }
  36. let users = [{
  37. name: "Иван",
  38. age: 17,
  39. }, {
  40. name: "Мария",
  41. age: 35,
  42. }, {
  43. name: "Алексей",
  44. age: 73,
  45. }, {
  46. name: "Яков",
  47. age: 12,
  48. }]
  49. users.sort(byField('age', true)); //сортирует по возрасту по возрастанию
  50. users.sort(byField('name', true)); //сортирует по имени по убыванию
  51. // array map
  52. let Array = ["1", {}, null, undefined, "500", 700];
  53. let result = Array.map(function(item) {
  54. let number = parseInt(item);
  55. return isNaN(number)? item : number;
  56. })
  57. console.log(result);
  58. // array reduce
  59. let arr = ["0", 5, 3, "string", null];
  60. let result = arr.reduce(function(previousValue, currentValue) {
  61. return typeof currentValue == 'number' ? previousValue * currentValue : previousValue;
  62. },1);
  63. console.log(result);
  64. // object filter
  65. function filter(obj, callback) {
  66. let mainObj = {};
  67. for(let key in obj) {
  68. if (callback(key, obj[key])) {
  69. mainObj[key] = obj[key];
  70. }
  71. }
  72. return mainObj;
  73. }
  74. let phone = {
  75. brand: "meizu",
  76. model: "m2",
  77. ram: 2,
  78. color: "black",
  79. }
  80. let outcome = filter(phone,(key,value) => key == "color" || value == 2);
  81. console.log(outcome);
  82. // object map
  83. function map(object, callback) {
  84. let obj = {};
  85. for(let key in object)
  86. Object.assign(obj,callback(key, object[key]));
  87. return obj;
  88. }
  89. let obj = map({name: 'Иван', age: 17}, function(key, value) {
  90. let result = {};
  91. result[key+"_"] = value + "$";
  92. return result;
  93. })
  94. console.log(obj);
  95. //возвращает {name_: "Иван$", age_: "17$"}
  96. // Sum
  97. // pешение с помощью цикла
  98. function sum(n) {
  99. let sum = 0;
  100. for (let i = 1;i <= n;i++) {
  101. sum += i;
  102. }
  103. return sum;
  104. }
  105. // or
  106. // pешение через рекурсию
  107. function sum(n) {
  108. if (n == 1) return 1;
  109. return n + sumTo(n - 1);
  110. }