main.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // //cube
  2. // let cube = n => n*n*n
  3. // alert(cube(10))
  4. // //avg2
  5. // let avg2 = (a,b) => (a+b)/2
  6. // alert (avg2 (10,7))
  7. // //sum3
  8. // let sum3 = (a,b,c) => {
  9. // a = a || 0;
  10. // b = b || 0;
  11. // c = c || 0;
  12. // var result = a+b+c
  13. // alert (result)
  14. // }
  15. // sum3(10,5,17)
  16. // sum3(10,5)
  17. // //intrandom
  18. // let intRandom = (low,high) =>Math.round(Math.random()*((high || 0) - (low ||0)) + (low || 0))
  19. // alert(intRandom(10,5))
  20. // //greetAll
  21. // let greetAll = (arguments) => {
  22. // let str = " "
  23. // for (let i = 0;i < arguments.length;i++){
  24. // str += arguments[i]
  25. // }
  26. // str.slice(0 , -1)
  27. // alert ("Hello" + "," + str)
  28. // }
  29. // greetAll("SuperMan")
  30. // //sum (//nerabotaet)
  31. // let sum = (arguments) =>{
  32. // var result = 0;
  33. // for (var i = 0;i <arguments.length; i++){
  34. // result += arguments[i]}
  35. // }
  36. // alert(sum(1,5,6,7,10))
  37. //sort
  38. let persons = [
  39. {name: "Иван", age: 17},
  40. {name: "Мария", age: 35},
  41. {name: "Алексей", age: 73},
  42. {name: "Яков", age: 12},
  43. ]
  44. function sort(field, boolean = true) {
  45. if (boolean) {
  46. return (a, b) => a[field] > b[field] ? 1 : -1
  47. }else {
  48. return (a, b) => a[field] < b[field] ? 1 : -1
  49. }
  50. }
  51. console.log(persons.sort(sort('age', false)))
  52. //arr.map
  53. let arr = ["1", {}, null, undefined, "500", 700];
  54. let newArr = arr.map(function(a){
  55. return typeof(a) === 'string' ? parseInt(a) : a;
  56. });
  57. console.log(arr);
  58. console.log(newArr);
  59. //arr.reduce
  60. var arr = ["0", 5, 3, "string", null]
  61. var result = arr.reduce(function(previousValue, currentValue){
  62. if(typeof(currentValue)==='number'){
  63. previousValue*=currentValue;
  64. };
  65. return previousValue
  66. }, 1)
  67. alert(result);
  68. //object filter
  69. var phone = {
  70. brand: "meizu",
  71. model: "m2",
  72. ram: 2,
  73. color: "black",
  74. };
  75. function filter (object , name ,value) {
  76. let result = {}
  77. for (key in object) {
  78. if (name == key || object[key] == value){
  79. result[key] = object [key]
  80. }
  81. }
  82. return result
  83. }
  84. console.log (filter(phone,'color',2))
  85. //object map
  86. let obj = map({name: "Иван", age: 17},function(key,value){
  87. var result = {};
  88. result[key+"_"] = value + "$";
  89. return result;
  90. })
  91. function map (object , callback){
  92. let obj = {}
  93. for(let key in object) {
  94. Object.assign(obj,callback(key, object[key]));
  95. }
  96. return obj
  97. }
  98. console.log(obj);
  99. //рекурсия (считает сумму до какого-то числа)
  100. function sumTo(n) {
  101. if (n == 1) return 1;
  102. return n + sumTo(n - 1);
  103. }
  104. alert( sumTo(10) );