index.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. //ES6 done hw7
  12. //sort
  13. var persons = [
  14. {name: "Иван", age: 17},
  15. {name: "Мария", age: 35},
  16. {name: "Алексей", age: 73},
  17. {name: "Яков", age: 12},
  18. ]
  19. function sort(array, keys, boolean){
  20. array.sort(sorting)
  21. function sorting(a, b){
  22. return (boolean) ? (a[keys] < b[keys]) : (b[keys] < a[keys]);
  23. }
  24. return array;
  25. }
  26. console.log(sort(persons, "age"));
  27. // console.log(sort(persons, "name", false));
  28. // console.log(sort(persons, "age",true));
  29. //array map
  30. var array = ["1", {}, null, undefined, "500", 700]
  31. var result = array.map(function(item) {
  32. var number = parseInt(item);
  33. return isNaN(number)? item : number;
  34. });
  35. console.log(result);
  36. //array reduce
  37. var arr = ["0", 5, 3, "string", null]
  38. function reducing(total, i){
  39. if (!isNaN(i) && i != 0 && isFinite(i) && i != null){
  40. return total*=i
  41. }
  42. return total;
  43. };
  44. var result2 = arr.reduce(reducing, 1)
  45. console.log( result2 )
  46. //object filter
  47. var phone = {
  48. brand: "meizu",
  49. model: "m2",
  50. ram: 2,
  51. color: "black",
  52. };
  53. console.log(filter(phone,(key,value) => key == "color" || value == 2));
  54. function filter(object ,key , value){
  55. var result = {};
  56. for(var key in object){
  57. for(var value in object){
  58. if(object[key] == 2) result[key] = object[key];
  59. if(value == "color") result[value] = object[value];
  60. }
  61. }
  62. return result;
  63. }
  64. //object map
  65. map({name: "Иван", age: 17},function(key,value){
  66. var result = {};
  67. result[key+"_"] = value + "$";
  68. return result;
  69. })
  70. //должен вернуть {name_: "Иван$", age_:"17$"}
  71. function map(object, funct){
  72. for(key in object){
  73. funct(object, key, object[key]);
  74. delete object[key];
  75. }
  76. return object;
  77. }
  78. function check(object, key, value){
  79. object[key + "_"] = value + "$";
  80. return object;
  81. }
  82. console.log(map({name: "Иван", age: 17}, check))
  83. //Рекурсия +++++++++++
  84. function recurse(min , step , max){
  85. if (max <= 0) return 0;
  86. return min + recurse(min + step, step ,max -1)
  87. }
  88. console.log(recurse(5,10,15) + " = Сумма арифметической прогрессии Sn")
  89. </script>
  90. </body>
  91. </html>